Can we call member function only in member initialization list in c++?

1k views Asked by At

I want provide a class with a member function that will initialize the all member of class separately.

e.g.

#include <iostream>

using namespace std;
int x = 10;

class my{
  public:
    my():init{}
     int &i;
     void init()
     {
        i = x;
     }
};

int main()
{
  my m;
  return 0;
}

I know if I can use "class my : i(init())" will work, but I have some special purpose to intialize like above.

However in above example, I'm getting following error:

class ‘my’ does not have any field named ‘initMy’.

How to resolve this?

3

There are 3 answers

0
merlin2011 On

If you are trying to write a constructor for class my, then it must be named with the class name. The following will work assuming that initMy is the name of another class that you are trying to subclass.

class my : initMy
{
  public:
    int i;
    my() {
      i = 10;
    }
};
0
Basile Starynkevitch On

You might try to pre-initialize all the fields, then calling the initializing function inside the constructor:

class my {
 public:
  int i;
  void initMy() {
    i = 10;
  }
  my() : i(0) { initMy(); };
};

You could also (in C++11) define a bizarre signature for a private constructor, and delegate a constructor to it

class my {
  private:
    void initMy () { i=10; };
    enum privateen {privatev};
    my(enum privateen) : i(0) { initMy(); };
  public:
    my() : my(privatev) {};
    int i;
};

Actually, I believe that your initialization should be in a constructor, not in some other function.

3
Nayana Adassuriya On

Few things to clarify here.

  1. Member initialization list is for initialize members (mostly same purpose of the constructor).In initialize list nothing to do with member functions. in this example age(newAge) is not a function. It is initializing age variable.

class Man{
    private:
        int age;
        string name;

    public:
        Man(int newAge):age(newAge),name("Jhon"){}  
    };`

  1. You can use constructor to initialize the members of the class.

class Man{
private:
    int age;
    string name;

public:
    Man(int newAge)
    {
        age = newAge;
        name = "Jhone";
    }  
};

  1. Alternatively you can use a init method to do initialization, if you have some issue to use constructor.

class Man{
    private:
        int age;
        string name;

    public:
        Man(){}
        init(int newAge, string newName)
        {
            age = newAge;
            name = newName;
        }  
    };

  1. If you need to set the value of only one member in a class, you have to use a setter method

class Man{ private: int age; string name;

public:
    Man(){}
    setAge(newAge)
    {
     age = newAge;
    }
    setName(newName)
    {
      name = newNAme
    }

};

edit:

class Man{
    private:
        int age;
        string name;

    public:
        Man(initAge, initName)
        {
            setValues(initAge, initName);
        }

        setValues(newAge, newName)
        {
         age = newAge;
         name = newName;
        }        

    };

  int main()
  {
   Man goodMan(34,"Jhon");
   goodMan.setValues(45,"Kevin");
  }