Can't figure out how to initialize a class member which is an object

 
class A {
public:
   A(string c) {}
};

class B {
   A a;
};
			
// can't figure out how to get out of this code block either lol
// so the problem here is that you can't assign with = when declaring A a; you also can't use a C++ style {} initializer there
// I also can't figure out how to do it from a constructor of B

// how is this supposed to be done? I mean pass a string for the 'c' parameter inside of B for the 'a' member variable object
 
ycomp:
Use pointer A*a, and then in the B constructor a=new A("asd");
Don't forget to delete the pointer in B destructor.
 
ycomp:

As any other member, in the constructor, what is the problem ?

//Constructor 1
 B(string aString) : A(sString) {}

//Constructor 2
 B(void) : A("default") {}

Please read the documentation before asking on the forum.

 
Laszlo Tormasi #:
Use pointer A*a, and then in the B constructor a=new A("asd");
Don't forget to delete the pointer in B destructor.
You don't need a pointer.
 
Laszlo Tormasi #:
Use pointer A*a, and then in the B constructor a=new A("asd");
Don't forget to delete the pointer in B destructor.

thanks

then when I want to access a member `A`, `a->member` doesn't seem to work. is it safe to use `a.member` ? this compiles fine but i haven't run the code to test if it is valid or do I need to `(*a).member` (which also compiles fine) ?

 
ycomp #:

thanks

then when I want to access a member `A`, `a->member` doesn't seem to work. is it safe to use `a.member` ? this compiles fine but i haven't run the code to test if it is valid or do I need to `(*a).member` (which also compiles fine) ?

Please read the documentation before asking on the forum !

mql is not C++, there is no real pointer as in C++. mql is running in a managed environment, you can't manipulate memory directly with mql.

 
ycomp #:

thanks

then when I want to access a member `A`, `a->member` doesn't seem to work. is it safe to use `a.member` ? this compiles fine but i haven't run the code to test if it is valid or do I need to `(*a).member` (which also compiles fine) ?

Yes, a.member is safe.
 
ok thanks, I got it to work, I hadn't tried : a("str") in the constructor initializer list