0
class Foo
{
public:
  // single parameter constructor, can be used as an implicit conversion
  Foo (int foo) : m_foo (foo) 
  {
  }

  int GetFoo () { return m_foo; }

private:
  int m_foo;
};

m_foo is an integer as defined in private section, but what's m_foo(foo)? that looks like a function.

is m_foo both an integer and a function? How does that work?

And Foo(int foo) contructor is extending the m_foo function.

lilzz
  • 5,113
  • 13
  • 53
  • 83

4 Answers4

5
Foo (int foo) : m_foo (foo) 

This is an initializer list. It initialises m_foo to have the value foo.

David Heffernan
  • 587,191
  • 41
  • 1,025
  • 1,442
2

You are initializing a integer variable using initializer list. Essentially before you enter the body of the constructor m_foo is assinged to foo.

Ivaylo Strandjev
  • 66,530
  • 15
  • 117
  • 170
1

It is an intializer. It sets the value of the m_foo item by calling it's copy-constructor (instead of creating a temporary object and then calling the copy-constructor if you were to set it in the constructor like m_foo = foo).

Zac Howland
  • 15,485
  • 1
  • 24
  • 40
-1

I'm not sure that basic questions about C++ have their place here, however:

Foo (int foo) : m_foo (foo) 

means: define a constructor, and initialize the member variable m_foo with the foo formal argument.

Basile Starynkevitch
  • 216,767
  • 17
  • 275
  • 509