Thread: Constructor problem

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    28

    Question Constructor problem

    In the following code, I'm trying to create a class that contains a TextField. In this case, the TextField default constructor is just fine for my needs, as it supplies it with some default dimensions, but what if I want a TextField with different dimensions?

    Code:
    // graphics controller class
    class GraphCon : public MessageReceiver<GraphConMessage *> {
    protected:
         TextField field;
    };
    It won't let me do
    Code:
    TextField field(12,14,'s');
    in the class definition, even though this seems to be legal in normal code. Is there any way to make the TextField with something besides the default constructor without using 'new'?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Use the initializer list in your class constructor(s):
    Code:
    class GraphCon : public MessageReceiver<GraphConMessage *> {
    public:
       GraphCon() : field(12,14,'s') { /* ... */ }
    protected:
       TextField field;
    };
    This is a good idea in general for all member variables.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Quote Originally Posted by Daved
    Use the initializer list in your class constructor(s):
    Code:
    class GraphCon : public MessageReceiver<GraphConMessage *> {
    public:
       GraphCon() : field(12,14,'s') { /* ... */ }
    protected:
       TextField field;
    };
    This is a good idea in general for all member variables.
    This means that when you class is constructed ( GraphCon ), the member variable field will be constructed with the parameters specified, or in other word that how it's constructor will be called.

Popular pages Recent additions subscribe to a feed