Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1]   Go Down

Author Topic: Confused about this syntax >_>  (Read 1723 times)

0 Members and 1 Guest are viewing this topic.
Confused about this syntax >_>
« on: July 20, 2009, 05:54:08 pm »
  • Doesn't afraid of anything
  • *
  • Reputation: +42/-0
  • Offline Offline
  • Gender: Male
  • Posts: 7002
I've been confused about this one for quite some time now.  None of the textbooks I've used have ever explained what it means.  The line in question is:  MyClass(int i) : m_i(i) {}

Code: [Select]
class MyClass {
public:
   MyClass(){}
   MyClass(int i) : m_i(i) {}

private:
   int m_i;
};
Logged



i love big weenies and i cannot lie
Re: Confused about this syntax >_>
« Reply #1 on: July 20, 2009, 08:45:39 pm »
  • You don't know me.
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 234
I'm not entirely sure what this specific case means, but I know something similar that might help. When you have a child class and you want to call the parent class' constructor and pass it some variables, you'd do something like

Code: [Select]
childConstructor(int variable) : parentConstructor(variable)
{
       // Child constructor code
}

I guess here it's the same thing, only m_i is not actually related or something. Again, I'm not sure, I just noticed that it looked similar. I hope that helps.
Logged


No one located in this area of the internet remembers me.
Re: Confused about this syntax >_>
« Reply #2 on: July 20, 2009, 09:17:04 pm »
  • Doesn't afraid of anything
  • *
  • Reputation: +42/-0
  • Offline Offline
  • Gender: Male
  • Posts: 7002
Now, see that makes sense, but what I don't get is that m_i is a variable, yet it appears to have a parameter passed to it.
Logged



i love big weenies and i cannot lie
Re: Confused about this syntax >_>
« Reply #3 on: July 20, 2009, 09:42:46 pm »
  • *
  • Reputation: +8/-0
  • Offline Offline
  • Gender: Male
  • Posts: 6604
The easiest way to clear confusion is to test the code yourself. Thinking about all it could do also helps, anyway running this:
Code: [Select]
#include <iostream>

using std::cin;
using std::cout;
using std::endl;

class MyClass
{
  public:
    MyClass();
    MyClass(int i) : m_i(i) {}
    void show_mi(){ cout << m_i; }
  private:
    int m_i;
};

int main(){
  MyClass Test(3);
  Test.show_mi();
  system("PAUSE");
  return 0;
};

I figured out that it sets the variable.

It's probably also listed somewhere in here: http://www.research.att.com/~bs/C++.html
« Last Edit: July 20, 2009, 09:45:37 pm by 4Sword »
Logged
Re: Confused about this syntax >_>
« Reply #4 on: July 20, 2009, 10:06:21 pm »
  • *
  • Reputation: +3/-0
  • Offline Offline
  • Gender: Male
  • Posts: 6629
Its pretty much just a shorthand way of setting fields. Nothing particularly intresting about it.
Logged
Re: Confused about this syntax >_>
« Reply #5 on: July 20, 2009, 10:10:04 pm »
  • (y)(;>.<;)(y)
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3293
It invokes the constructor, not the assignment. Whilst this means nothing really for variables like integers, this can be all the difference in the world for classes.

It's like doing: int ten(10);
instead of int ten = 10;
Logged
Re: Confused about this syntax >_>
« Reply #6 on: July 20, 2009, 11:00:18 pm »
  • Doesn't afraid of anything
  • *
  • Reputation: +42/-0
  • Offline Offline
  • Gender: Male
  • Posts: 7002
Oh....I..don't quite see the point of that..but at least that clears it up.  Thanks =)
Logged



i love big weenies and i cannot lie
Re: Confused about this syntax >_>
« Reply #7 on: July 21, 2009, 09:47:06 am »
  • (y)(;>.<;)(y)
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3293
Well it looks a bit neater according to some people, especially if otherwise your constructor would be empty, and like I said for objects can be vital.

Also it's the only way to assign value to constant variables in a class (i.e if instead of int you have a const int).
Logged
Pages: [1]   Go Up

 


Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved



Page created in 0.014 seconds with 50 queries.

anything