0

I have the following code:

#include <iostream>
using namespace std;

struct S {
    S(int i): I(i) { }
    int & v () { return I; }
    private :
    int I;
};

S s1 (1);
int main() {

    cout << s1.v() << "\n";

    return 0;
}

And I get the output:

1

I am confused as to how it is working. For example with s1 it is calling the constructor S(int i) but how is it sub-classing I and calling its constructor when there is no I class and how is the private variable I getting assigned a number when there has been nothing assigned to it? Also, if v() returns int& (a reference therefore, I would think it would print out a memory location but it gives 1 consistently).

Sorry if this sounds stupid can't figure it out.

Dair
  • 15,910
  • 9
  • 62
  • 107

3 Answers3

3

how is it sub-classing I and calling its constructor when there is no I class and how is the private variable I getting assigned a number when there has been nothing assigned to it?

The syntax

S(int i): I(i) { }

means construct S by assigning the member I the value of the parameter i. You could implement it like this instead:

S(int i) { I = i; }

if v() returns int& (a reference therefore, I would think it would print out a memory location but it gives 1 consistently).

The reference return by v is a reference to the member I of s1. As long as s1 hasn't been deallocated the reference is valid. In your code s1 it is a static variable that only is deallocated when the program exits.

Martin Liversage
  • 104,481
  • 22
  • 209
  • 256
2

It seems you have multiple questions:

  1. The struct is not subclassing I, it is simply using Initialization Lists to construct the I variable. The same could have been done with the constructor S(int i) { I = i;}.

  2. It is getting assigned in the constructor, see #1.

  3. You are confusing references and pointers. References pretend to act like regular value passed numbers. Pointers, returning int * with a function such as int * v () { return &I; } would print out the address of the variable unless you dereference them with the * symbol. References automatically dereference themselves.

  • Thanks, it makes sense now except: why not just make the return type `int` any diff? – Dair Aug 21 '11 at 00:43
  • anon: When you return int&, you can change the value of the variable through the function. Like you can do `si.v() = 3`. This use was one of the main reasons for introducing references to the language. –  Aug 21 '11 at 00:45
  • It's not being assigned anywhere, the advantage of the ctor-initializer syntax is that it performs initialization not assignment. – Ben Voigt May 11 '14 at 02:07
0

Nothing is being subclassed. I recommend you pick up a good C++ book and read up a bit. A class (or struct) in C++ has access specifiers public, protected and private, which control the access level to everything following the specifier:

class A
{
  /* private by default */
public:
  /* public stuff */
private:
  /* private again */
};

struct B
{
  /* public by default */
private:
  /* private stuff */
public:
  /* public stuff again */
private:
  /* private stuff again */
};

Your class S simply contains a private member int I which is initialized to 1 by the constructor and then printed.


The syntax for inheritance is a colon in the class declaration:

class  Foo : public Bar,
             protected FooBase,
             private SomeHelper
{ /* ... */ };
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084