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
hvelascoAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
AxterConnect With a Mentor Commented:
Example:

class MyClass2 : public MyClass1
{
    public:
       MyClass2(const string &nameinput, int number);
    private:
    static string InitiateName(const string &nameinput, int number)
    {
         string ProcessString = nameinput;
         //Code to process nameinput with number
         if (number)
        {//Processing code here
        }
         return ProcessString;
    }
 }

MyClass2::MyClass2(const string &nameinput, int number):MyClass1(InitiateName(nameinput, number))
{

0
 
AxterCommented:
Hi hvelasco,
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!
0
 
jkrCommented:
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);
}
0
 
AxterCommented:
>>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.
0
 
jkrCommented:
>>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);
}
0
All Courses

From novice to tech pro — start learning today.