References question

A

Anunay

Hello all,

I have a doubt regarding references. Consider the following program:

class Test
{
int &i;
int &j;
int &k;
};

int main()
{

cout<< "size of class Test = " << sizeof(class Test) <<endl;
return 0;
}

On my machine. this prints 12 i.e size of 3 pointers. What i know about
references is that, we need to initialize them at the time of
declaraion. But, here we are not doing so. We are not defining a
constructor also. I was expecting a compilation error saying that I
need to initialize i,j and k. But instead, I got 12!

Kindly explain this behaviour.

Thanks,
Anunay
 
I

Ian Collins

Anunay said:
Hello all,

I have a doubt regarding references. Consider the following program:

class Test
{
int &i;
int &j;
int &k;
};

int main()
{

cout<< "size of class Test = " << sizeof(class Test) <<endl;
return 0;
}

On my machine. this prints 12 i.e size of 3 pointers. What i know about
references is that, we need to initialize them at the time of
declaraion. But, here we are not doing so. We are not defining a
constructor also. I was expecting a compilation error saying that I
need to initialize i,j and k. But instead, I got 12!

Kindly explain this behaviour.
sizeof doesn't instantiate an object, it just gives the size.

But I think you should still get an error. Which compiler?
 
M

Markus Schoder

Anunay said:
Hello all,

I have a doubt regarding references. Consider the following program:

class Test
{
int &i;
int &j;
int &k;
};

int main()
{

cout<< "size of class Test = " << sizeof(class Test) <<endl;
return 0;
}

On my machine. this prints 12 i.e size of 3 pointers. What i know about
references is that, we need to initialize them at the time of
declaraion. But, here we are not doing so. We are not defining a
constructor also. I was expecting a compilation error saying that I
need to initialize i,j and k. But instead, I got 12!

As long as you do not actually create an object of type Test there is
no reason for an error. The size of the object is known from its
declaration alone.
 
W

wallaby.curious.about.it

Anunay said:
Hello all,

I have a doubt regarding references. Consider the following program:

class Test
{
int &i;
int &j;
int &k;
};

int main()
{

cout<< "size of class Test = " << sizeof(class Test) <<endl;
return 0;
}

On my machine. this prints 12 i.e size of 3 pointers. What i know about
references is that, we need to initialize them at the time of
declaraion. But, here we are not doing so. We are not defining a
constructor also. I was expecting a compilation error saying that I
need to initialize i,j and k. But instead, I got 12!

Kindly explain this behaviour.

Thanks,
Anunay

Memory for non-dynamic members is allocated before constructor
starts... And certainly you have a default constructor in this cute
class. References use memory too, like pointers... They should have
been initialized, because this way they do nothing else but consuming
memory. You coud initialize them like here:
class Test
{
int &i;
int j;
Test():i(j){}
};

Best regards
 
A

Anunay

Memory for non-dynamic members is allocated before constructor
starts... And certainly you have a default constructor in this cute
class. References use memory too, like pointers... They should have
been initialized, because this way they do nothing else but consuming
memory. You coud initialize them like here:
class Test
{
int &i;
int j;
Test():i(j){}
};

How will we write the constructor for this class, assuming that there
are no other data members in this class, just these 3 references?
 
T

Tomás

References use memory too, like pointers...


Not necessarily. The implementation is free to implement references however
it pleases.

Here's a reference which wouldn't use memory:


class SomeClass {
private:

int prv_i;


public:

const int &i;

SomeClass() : i(prv_i) {}

};


-Tomás
 
A

Alan Johnson

Anunay said:
Can the initialization of the reference can be done without declaring
any other data member?

You can initialize the reference to refer to anything you'd like. For
example:

class test
{
private:
int &ref ;

public:
test(int & i)
: ref(i)
{}
} ;

int main()
{
int i = 42 ;
test t(i) ;
}
 
B

benben

How will we write the constructor for this class, assuming that there
are no other data members in this class, just these 3 references?

class Test {
int &i;
int &j;
int &k;

public:
Test(int& a, int& b, int& c)
:i(a), j(b), k(c)
{}
};


Ben
 
A

Anunay

benben said:
class Test {
int &i;
int &j;
int &k;

public:
Test(int& a, int& b, int& c)
:i(a), j(b), k(c)
{}
};


Ben

Thanks Ben and Alan, that was what I was looking for.
 
A

Anunay

benben said:
class Test {
int &i;
int &j;
int &k;

public:
Test(int& a, int& b, int& c)
:i(a), j(b), k(c)
{}
};


Ben

Thanks Ben and Alan, that was what I was looking for.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top