CPP Syntax
-
Hi, I have a Class, which contains as member another class. This latter contained class has no default constructor, but requires a Parameter in it's constructor. How do I call this latter constructor in the constructor of the first class. This is a General CPP Question, but it is a bit abstract, so I give a Concrete example: For instance: I have a class derived from CDialog. It contains four instances of a custom class CDragBar. The CDragBar class in it's constructor determines whether it is horizontal or vertical. (one cannot have an indeterminate one) In this case I know ways around this conundrum,(e.g. Provide a default constructor for the CDragBar class, and provide methods like SetVertical() and SetHorizontal(), to be called in the 'CDialog derived classes constructor') but wonder is there a CPP syntax that could be used instead. The example I gave is in a way quite trivial. The Colon separated Constructor List only works for base class constructors of the main classes constructor in Microsoft Compilers, and not for contained member classes. Anyone any ideas? Even if the idea is that no such syntax exists. Kind Regards,
Bram van Kampen
-
Hi, I have a Class, which contains as member another class. This latter contained class has no default constructor, but requires a Parameter in it's constructor. How do I call this latter constructor in the constructor of the first class. This is a General CPP Question, but it is a bit abstract, so I give a Concrete example: For instance: I have a class derived from CDialog. It contains four instances of a custom class CDragBar. The CDragBar class in it's constructor determines whether it is horizontal or vertical. (one cannot have an indeterminate one) In this case I know ways around this conundrum,(e.g. Provide a default constructor for the CDragBar class, and provide methods like SetVertical() and SetHorizontal(), to be called in the 'CDialog derived classes constructor') but wonder is there a CPP syntax that could be used instead. The example I gave is in a way quite trivial. The Colon separated Constructor List only works for base class constructors of the main classes constructor in Microsoft Compilers, and not for contained member classes. Anyone any ideas? Even if the idea is that no such syntax exists. Kind Regards,
Bram van Kampen
Do you mean something like
#include using namespace std;
class A
{
int _i;
public:
A(int i):_i(i){}
int get() const {return _i;}
};class B
{
A _a;public:
B():_a(10){}
B(int i):_a(i){}const A & get(){ return _a;}
};int main()
{
B b0;
B b1(1);
cout << b0.get().get() << ", " << b1.get().get() << endl;
} -
Do you mean something like
#include using namespace std;
class A
{
int _i;
public:
A(int i):_i(i){}
int get() const {return _i;}
};class B
{
A _a;public:
B():_a(10){}
B(int i):_a(i){}const A & get(){ return _a;}
};int main()
{
B b0;
B b1(1);
cout << b0.get().get() << ", " << b1.get().get() << endl;
}Nice, I think you need to try this source: Selection, Insertion, Bubble and Heap Sort in C++ | Codinglio[^] :) :)
-
Do you mean something like
#include using namespace std;
class A
{
int _i;
public:
A(int i):_i(i){}
int get() const {return _i;}
};class B
{
A _a;public:
B():_a(10){}
B(int i):_a(i){}const A & get(){ return _a;}
};int main()
{
B b0;
B b1(1);
cout << b0.get().get() << ", " << b1.get().get() << endl;
}Well, The issue is very similar to that example. I note the curly braces and the absence of a terminating semi colon. What is the reasoning behind the syntax. Does that syntax intentionally leave out the terminating semi colon, or, is that an oversight? Going to try that! Thanks and Regards :)
Bram van Kampen