473,406 Members | 2,208 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

References question

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

Jun 2 '06 #1
10 1591
Anunay wrote:
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?

--
Ian Collins.
Jun 2 '06 #2
Anunay wrote:
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.

Jun 2 '06 #3

Anunay wrote:
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

Jun 2 '06 #4

wa**********************@gmail.com wrote:
Anunay wrote:
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;
}
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?

Jun 2 '06 #5
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
Jun 2 '06 #6

Tomás wrote:
int prv_i;


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

Jun 2 '06 #7
Anunay wrote:
Tomás wrote:
int prv_i;


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) ;
}
--
Alan Johnson
Jun 2 '06 #8
> 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
Jun 2 '06 #9

benben wrote:
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


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

Jun 5 '06 #10

benben wrote:
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


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

Jun 5 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jim R | last post by:
This is a really really dumb question but one that keeps bugging me. How do you know which references and components have to be in your project. I went through hell trying to find the right...
4
by: Mark D. Anderson | last post by:
About a month ago Richard Cornford did an interesting analysis of a memory leak in jscript (internet explorer) when there are "circular" references between DOM objects and (real) jscript objects:...
2
by: Michelle Collier-Moore | last post by:
Please could someone offer some advice regarding adding references to an Access database? I tried to open a project a few days ago sent to me by someone whose developer had left the company. I...
3
by: SAM | last post by:
Hi everyone, I consider myself a very competent programmer when it comes to actual programming and analyzing the business that I'm modelling. I am now crossing into what I would consider Access...
3
by: S. van Beek | last post by:
Dear reader, I still have a problem with my reference libraries. In my frond end application a check procedure for missing references is available. The problem I confronted with is that...
2
by: S. van Beek | last post by:
Dear reader, For removing a reference in the VBA reference form I receive from Doug Steele the following code: ........... References.Remove refCurr
6
by: Gary James | last post by:
This may not be a direct C# question, but since I'll be using using C# for development, I thought I'd pose the question here. I'll soon be involved in the design of a new software product that...
11
by: codebloatation | last post by:
I know how to use references but i DO not get WHY they exist other than to add to the language. Are they actually needed for anything?
9
by: igor.kulkin | last post by:
References is a relatively basic feature of C++ language. It might be a good thing to think of references as aliases to the variables. However it's good to think of references this way when you...
19
by: MQ | last post by:
Can someone tell me where I should use pointers and where I should use references? In his book, Stroustrup says that you should use pointers for passing arguments that are to be modified, not...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.