hvelasco
asked on
How to initialize a member
Dear Experts,
I have the following problem:
class MyClass1
{
public:
MyClass1(const string &name);
private:
string m_name;
int m_one;
}
MyClass1::MyClass1(const string &name):m_name(name)
{
}
class MyClass2 : public MyClass1
{
public:
MyClass2(const string &nameinput, int number);
}
MyClass2::MyClass2(const string &nameinput, int number):MyClass1(nameinput )
{
if (number)
{
HERE I WOULD LIKE TO INITIALIZE m_one FROM MyClass1
I CAN NOT INITIALIZE ABOVE BECAUSE I NEED TO DO SOMETHING WITH number
BEFORE ASSIGN VALUE. ALSO I DO NOT WANT TO SET m_one STATIC BECAUSE
I DO NOT WANT ONE COPY FOR ALL OBJECTS
any help will be greatly appreciated.
Thank you
I have the following problem:
class MyClass1
{
public:
MyClass1(const string &name);
private:
string m_name;
int m_one;
}
MyClass1::MyClass1(const string &name):m_name(name)
{
}
class MyClass2 : public MyClass1
{
public:
MyClass2(const string &nameinput, int number);
}
MyClass2::MyClass2(const string &nameinput, int number):MyClass1(nameinput
{
if (number)
{
HERE I WOULD LIKE TO INITIALIZE m_one FROM MyClass1
I CAN NOT INITIALIZE ABOVE BECAUSE I NEED TO DO SOMETHING WITH number
BEFORE ASSIGN VALUE. ALSO I DO NOT WANT TO SET m_one STATIC BECAUSE
I DO NOT WANT ONE COPY FOR ALL OBJECTS
any help will be greatly appreciated.
Thank you
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Actually, since MyClass2 is derived from MyClass1, it will inherit m_one also. All you need to do is - if you want to use Axter's 'InitiateName()' -style processing (which I don't think needs to be static) and
int MyClass2::DoProcessing ( int n) {
return n + 42;
}
MyClass2::MyClass2(const string &nameinput, int number)
{
m_name = nameinput;
m_one = DoProcessing ( number);
}
int MyClass2::DoProcessing ( int n) {
return n + 42;
}
MyClass2::MyClass2(const string &nameinput, int number)
{
m_name = nameinput;
m_one = DoProcessing ( number);
}
>>Actually, since MyClass2 is derived from MyClass1, it will inherit m_one also.
Since the parent class has m_one private, you can't compile the above code.
>>(which I don't think needs to be static) and
It doesn't have to be static, but to make it safer, it should be static.
Any functions that is called on the intialize list, should avoid using the object's data members.
That's because the object is not fully formed yet, so the data members may not be initialize at that point.
By declaring the function static, you insure that you don't accidently try to use a data member.
So it makes the code safer by making it a static member method.
Since the parent class has m_one private, you can't compile the above code.
>>(which I don't think needs to be static) and
It doesn't have to be static, but to make it safer, it should be static.
Any functions that is called on the intialize list, should avoid using the object's data members.
That's because the object is not fully formed yet, so the data members may not be initialize at that point.
By declaring the function static, you insure that you don't accidently try to use a data member.
So it makes the code safer by making it a static member method.
>>Since the parent class has m_one private, you can't compile the above code
That's right - there is no need for that to be 'private', 'protected' will do fine:
class MyClass1
{
public:
MyClass1(const string &name);
protected:
string m_name;
int m_one;
}
MyClass1::MyClass1(const string &name):m_name(name)
{
}
class MyClass2 : public MyClass1
{
public:
MyClass2(const string &nameinput, int number);
int DoProcessing ( int n);
}
int MyClass2::DoProcessing ( int n) {
return n + 42;
}
MyClass2::MyClass2(const string &nameinput, int number)
{
m_name = nameinput;
m_one = DoProcessing ( number);
}
That's right - there is no need for that to be 'private', 'protected' will do fine:
class MyClass1
{
public:
MyClass1(const string &name);
protected:
string m_name;
int m_one;
}
MyClass1::MyClass1(const string &name):m_name(name)
{
}
class MyClass2 : public MyClass1
{
public:
MyClass2(const string &nameinput, int number);
int DoProcessing ( int n);
}
int MyClass2::DoProcessing ( int n) {
return n + 42;
}
MyClass2::MyClass2(const string &nameinput, int number)
{
m_name = nameinput;
m_one = DoProcessing ( number);
}
You can create a member function that will return value to initiate, and pass that member function to your parent class in the initialize list
continue....
Cheers!