A c++ question
-
I am declaring a class in a header file something like: /////////////// headerfile A.h //////////// #include "B.h" class A { private: B val; // Blah } As can be seen above, i've declared a class 'A' in its header file. I've created an object of class 'B' as a private member of class 'A'. Class 'B' also got a 'zero argument constructor'. As such will it be get called ??? How can i make sure that 'zero argument constructor' of class B, must be get called before i use it in any method of class A ???
-
I am declaring a class in a header file something like: /////////////// headerfile A.h //////////// #include "B.h" class A { private: B val; // Blah } As can be seen above, i've declared a class 'A' in its header file. I've created an object of class 'B' as a private member of class 'A'. Class 'B' also got a 'zero argument constructor'. As such will it be get called ??? How can i make sure that 'zero argument constructor' of class B, must be get called before i use it in any method of class A ???
when an object of type A is created, the constructors of all member variables will be called before A's constructor is called. try it.
class B
{
public:
B()
{
printf("B\n");
}
};class A
{
public:
A()
{
printf("A\n");
}B b;
};main()
{
A a;
}the output is : B A -c
A | B - it's not a choice.