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,243
  • 14
  • 59
  • 87

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
  • 601,492
  • 42
  • 1,072
  • 1,490
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
  • 69,226
  • 18
  • 123
  • 176
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,777
  • 1
  • 26
  • 42
-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
  • 223,805
  • 18
  • 296
  • 547