-1

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?

balaji
  • 1,075
  • 3
  • 12
  • 26
  • @All Now I edited the class code, could you please look it now. – balaji Dec 19 '14 at 06:39
  • 1
    A function cannot initialize members, it can only assign new values to already initialized ones. The only way to explicitly initialize a data member is to list it in a bases-members-initializer, and the only things you can put in a bases-members-initializer are data members and bases and (in C++11) other constructors. You can call a member function in the constructor body. – n. m. could be an AI Dec 19 '14 at 07:23
  • what is `initMy()`? where is the definition for that? – Nayana Adassuriya Dec 19 '14 at 07:23

3 Answers3

1

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;
    }
};
merlin2011
  • 71,677
  • 44
  • 195
  • 329
0

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.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
0

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");
  }
Nayana Adassuriya
  • 23,596
  • 30
  • 104
  • 147
  • Your all scenarios are correct. What I want is that a generic method used while initializing the class as well It can be invoked after construction to reload member variables with different value. is it possible? – balaji Dec 19 '14 at 06:50
  • of course. declare function to initialize the variables and call it in the constructor. after that you can call same function anytime. check my edit part – Nayana Adassuriya Dec 19 '14 at 06:53
  • That if fine to call the init method in constructor, but If my member variables are references then? We must have to initialize them in the member initialization list, in this case, How we can use init() method? – balaji Dec 19 '14 at 06:56