?
Solved

How to initialize a member

Posted on 2005-03-04
5
Medium Priority
?
163 Views
Last Modified: 2010-04-01
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
0
Comment
Question by:hvelasco
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 13460230
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
 
LVL 30

Accepted Solution

by:
Axter earned 2000 total points
ID: 13460269
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
 
LVL 86

Expert Comment

by:jkr
ID: 13460512
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
 
LVL 30

Expert Comment

by:Axter
ID: 13460575
>>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
 
LVL 86

Expert Comment

by:jkr
ID: 13460613
>>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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question