initializing a object in an another class object .(after doing some operations on that constructor.)
Asked Answered
I

1

2

I want to initialize a class member which is also another class object.The problem is that, I have to initialize the member with variables that I figure out after doing some operations on my constructor. Let me show the sample code.

class Child_class
{
    private:
        int var1, var2, var3;

        public:
        DateTime(int var1 = 1970, int var2 = 1, int var3 = 1);

};

second class:

class Owner_class
{
    private:

        Child_class foo;

    public:
        // I have to make some string split operations on string_var variable
        // and get some new variables.After that, I need to initialize child_class with  new variables               
        Owner_class( string string_var, int test);
}

One way to do that, I know I could write :

Owner_class::Owner_class():
   Child_class(new_var1,new_var2,new_var3) {
    // but since I'll find new_var1,new_var2 and new_var3 here.I couldnt use this method.
    // Am I right ?  
}

Is there anyone to help me ? thanks in advance!

Inversely answered 13/10, 2013 at 12:11 Comment(4)
No, you couldn't write that. You could write foo(new_var1,new_var2,new_var3) though.Craigcraighead
@LuchianGrigore I think doing it like that will cause a new local object instead of what I want to do.Inversely
Did you compile? Child_class(new_var1,new_var2,new_var3) in the initializer list will give you an error.Craigcraighead
@LuchianGrigore I didnt compile it because I know it gives me compile time errorInversely
B
2

You can write a function to do the calculations for you and return a Child_class object, and use this to initialize your instance in the Owner_class constructor initialization list:

Child_class make_child_object(string string_var, int test)
{
  // do stuff, instantiate Child_class object, return it
}

Then

Owner_class(string s, int n) : foo(make_child_object(s, n) {}

If this method is unsuitable, then an alternative is to give Child_class a default constructor, and assign it a value in the Owner_class constructor body:

Owner_class(string s, int n) 
{
  // foo has been default constructed by the time you gethere.
  // Do your stuff to calculate arguments of Child_class constructor.
  ....
  // construct a Child_class instance and assign it to foo
  foo = Child_class(a, b, c);
}
Bayless answered 13/10, 2013 at 12:14 Comment(15)
I have to make my calculations in the owner_class constructor.I use o ne of the string member variable of owner_class and I split it into 3 pieces.Using these 3 new variable I have to initialize Child_class object in the constructorInversely
@Inversely can't you perform those calculations in make_child_object?Bayless
make_child_object will be ownerClass method or childClass ? @BaylessInversely
@Inversely If possible, it should be a non-member function.Bayless
I couldnt do it like that @BaylessInversely
@Inversely why? You need to give reasons. I can't guess your requirements.Bayless
sorry, you're right. I have to follow some pattern whicj is given me. I could define new functions but just only in the classes. What if I do as a ownerClass method function ? Do you think it does work ? @BaylessInversely
@Inversely it could work, but you have to remember that in the constructor initialization list the object is not fully constructed. So calling non-static member functions from there is dangerous. If you must use a member function, make it static.Bayless
I more question, Is there any other place to initialize Child_class foo object in my owner constructor ? I mean Owner_class::Owner_class(){// do operations, and then foo(var1,var2,var3);} makes it correct or it just gives me a local object ? @BaylessInversely
@Inversely foo can only be initialized in the constructor initialization list. But you can modify a default constructed foo in the constructor body. This requires that you give Child_class a default constructor. See my edit.Bayless
thanks for reply, To do that, do I have to explictly write foo() before everything ? I mean Owner_class(string s,int n){ foo(), //do some calculations and after that foo=child_class(a,b,c)}Inversely
@Inversely no, you don't need foo() anywhere because it is a user defined type. If you were to add it, it would be in the constructor initialization list: Owner_class(string s, int n) : foo() { .... }, but this makes no difference here.Bayless
even if I dont write it anywhere, compile initializes it with default constructor right ?Inversely
@Inversely exactly. You would only have to write foo() explicitly if it were a built-in type (e.g. an int or a double or a bool etc) and you wanted it to be zero-initialized. But user defined types are always default constructed unless you call a different constructor in the initialization list.Bayless
I'm so greathful with your reply. I got it. Thanks again!Inversely

© 2022 - 2024 — McMap. All rights reserved.