9 Replies - 428 Views - Last Post: 10 November 2010 - 10:13 PM Rate Topic: -----

#1 heyoman1  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 64
  • View blog
  • Posts: 735
  • Joined: 30-November 09

What is this called and why do we need it?

Posted 10 November 2010 - 09:27 PM

(critter is a class, and m_Name is a private string)

Critter::Critter(const string &name;):m_Name(name) { }



What does this part mean???
:m_Name(name)



And why put an empty method ({})?
Is This A Good Question/Topic? 0
  • +

Replies To: What is this called and why do we need it?

#2 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: What is this called and why do we need it?

Posted 10 November 2010 - 09:38 PM

Quote

What does this part mean???
Initializer list. Which, as you might imagine, initializes the variables with the values in the parentheses.

Quote

And why put an empty method
Because there's nothing for the method to do?
Was This Post Helpful? 0
  • +
  • -

#3 heyoman1  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 64
  • View blog
  • Posts: 735
  • Joined: 30-November 09

Re: What is this called and why do we need it?

Posted 10 November 2010 - 09:50 PM

View PostOler1s, on 10 November 2010 - 08:38 PM, said:

Quote

What does this part mean???
Initializer list. Which, as you might imagine, initializes the variables with the values in the parentheses.

Quote

And why put an empty method
Because there's nothing for the method to do?


So why can't we just put { m_Name = name; }. It makes more sense than the way that I said before...
Was This Post Helpful? 0
  • +
  • -

#4 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: What is this called and why do we need it?

Posted 10 November 2010 - 09:52 PM

Quote

So why can't we just put { m_Name = name; }. It makes more sense than the way that I said before...
That's not initialization. That's assignment.
Was This Post Helpful? 0
  • +
  • -

#5 heyoman1  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 64
  • View blog
  • Posts: 735
  • Joined: 30-November 09

Re: What is this called and why do we need it?

Posted 10 November 2010 - 09:55 PM

View PostOler1s, on 10 November 2010 - 08:52 PM, said:

Quote

So why can't we just put { m_Name = name; }. It makes more sense than the way that I said before...
That's not initialization. That's assignment.


what's the difference between initializing and assigning? I know assigning means to give a variable a value, but so does initializing (I believe)...
Was This Post Helpful? 0
  • +
  • -

#6 Coding in the name of  Icon User is offline

  • D.I.C Head

Reputation: 67
  • View blog
  • Posts: 231
  • Joined: 07-July 10

Re: What is this called and why do we need it?

Posted 10 November 2010 - 10:00 PM

Const members must be initialized like in an initializer list. It's also used for calling the base class constructor in a derived constructor or initializing variables without default constructors.
Was This Post Helpful? 0
  • +
  • -

#7 heyoman1  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 64
  • View blog
  • Posts: 735
  • Joined: 30-November 09

Re: What is this called and why do we need it?

Posted 10 November 2010 - 10:05 PM

View PostCoding in the name of, on 10 November 2010 - 09:00 PM, said:

Const members must be initialized like in an initializer list. It's also used for calling the base class constructor in a derived constructor or initializing variables without default constructors.


I'm still confused. :-/
Was This Post Helpful? 0
  • +
  • -

#8 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: What is this called and why do we need it?

Posted 10 November 2010 - 10:07 PM

Quote

what's the difference between initializing and assigning?
Please check Google first before you ask a question...
Was This Post Helpful? 0
  • +
  • -

#9 Coding in the name of  Icon User is offline

  • D.I.C Head

Reputation: 67
  • View blog
  • Posts: 231
  • Joined: 07-July 10

Re: What is this called and why do we need it?

Posted 10 November 2010 - 10:08 PM

// No default constructor, must be passed a string on construction
class NonDef {
    public:
        NonDef(std::string s);
};

// Base class for Demo with some constructors
class DemoBase {
    public:
        DemoBase();
        DemoBase(int i);
        DemoBase(double d);
};

class Demo : public DemoBase {
    public:
        Demo()
          : DemoBase(5.0), constMember(10), nonDefaultMember("hi")
        {
        }

    private:
        const int constMember;    // Const value cannot be assigned, must be initialized
        NonDef nonDefaultMember;  // This cannot be default constructed
}

This post has been edited by Coding in the name of: 10 November 2010 - 10:11 PM

Was This Post Helpful? 0
  • +
  • -

#10 heyoman1  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 64
  • View blog
  • Posts: 735
  • Joined: 30-November 09

Re: What is this called and why do we need it?

Posted 10 November 2010 - 10:13 PM

View PostCoding in the name of, on 10 November 2010 - 09:08 PM, said:

// No default constructor, must be passed a string on construction
class NonDef {
    public:
        NonDef(std::string s);
};

// Base class for Demo with some constructors
class DemoBase {
    public:
        DemoBase();
        DemoBase(int i);
        DemoBase(double d);
};

class Demo : public DemoBase {
    public:
        Demo()
          : DemoBase(5.0), constMember(10), nonDefaultMember("hi")
        {
        }

    private:
        const int constMember;    // Const value cannot be assigned, must be initialized
        NonDef nonDefaultMember;
}

I think this cleared it up. It sets up the starting value for the variable only because they must be initialized from within a class.(?)

This post has been edited by heyoman1: 10 November 2010 - 10:15 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1