views:

111

answers:

3

I have a logic in base class constructor. The result of the logic has to be captured in the derived class constructor in a temporary variable. Is there a way to do it?

For example

class Base
{
   Base() { int temp_value = some_logic;  } 
};

class Derived : public Base
{
    Derived() { // need the temp value here.. }
};

Thanks, Gokul.

+2  A: 

I guess the easiest way I can think of would be to just separate some_logic into it's own method...

class Base
{
    Base() { int temp_value = initializationLogic();  }
    int initializationLogic(){ return some-logic;}
};

class Derived : public Base
{
    Derived() { int temp_value_here_too = initializationLogic(); }
};
filipe
The result would be that the initializationLogic() is invoked twice.
ChrisW
Thanks for the answer. Well i was not asking for code organization, if i am misunderstood. I was asking for a way to avoid the double calculation.
Gokul
But you *rejected* the way to avoid double calculation when you said you didn't want to add a field to the class. Storing the value *is* the way to avoid double calculation.
Jonathan Grynspan
@Jonathan: i don't want to store the value in the member, i am fine for a temporary variable to store the value
Gokul
There's no way to transfer a temporary value from the constructor of the base class down to the constructor of the subclass. You must ultimately add a field to the base class in order to maintain that information long enough for the subclass to see it. Is there a reason why you don't want to change the size of the class?
Jonathan Grynspan
@Jonathan Grynspan - Actually he did find a way: [see this answer here](http://stackoverflow.com/questions/3850408/c-reference-in-base-class-constructor/3850475#3850475). It's quite the hack.
ChrisW
Okay, that's just... a foul use of references.
Jonathan Grynspan
@Jonathan: So are you saying that the references should not be used in constructors??? Can you please explain??
Gokul
The way you've used them is... creative. It's not really the reason they were created, but I'm not going to criticize.
Jonathan Grynspan
+2  A: 

Either:

class Base 
{ 
   protected int not_so_temp_value;
   Base() { not_so_temp_value = some_logic_result;  }  
}; 

class Derived : public Base 
{ 
    Derived() { // read the not_so_temp_value member here.. } 
};

Or:

class Base 
{ 
   Base(int some_logic_result) { int temp_value = some_logic;  }  
}; 

class Derived : public Base 
{ 
    static Derived* create()
    {
       int some_logic_result = some_logic;
       return new Derived(some_logic_result);
    }
    Derived(int some_logic_result) : Base(some_logic_result)
    { // use the some_logic_result here.. } 
}; 
ChrisW
@Chris!! Thanks for the replies. First one increases the size. Second one asks for changing the constructor. Oh yeah!!! i can make the extra one as a default parameter. Thanks.....
Gokul
A: 

This is the one i am planning to use

class Base 
{ 
   Base(int& some_logic_result) { some_logic_result = some_logic;  }  
}; 

class Derived : public Base 
{ 
    Derived(int some_logic_result = 0) : Base(some_logic_result)
    { // use the some_logic_result here.. } 
}; 

Thanks, Gokul.

Gokul