473,320 Members | 1,861 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,320 software developers and data experts.

Problem with references



In class A I get a reference to 'SomeType':

class A {
SomeType test;
public:
A(SomeType& someT) {
test = someT;

}
...
...
};

But if I do operations on test the are not visible in the someT instance
since test is a local copy. But how do I make test a "reference copy"
of someT so when I change test I also change someT?
Sep 27 '07 #1
16 1227
meekee wrote:
In class A I get a reference to 'SomeType':

class A {
SomeType test;
public:
A(SomeType& someT) {
test = someT;
Don't assign. Initialise.
>
}
..
..
};

But if I do operations on test the are not visible in the someT
instance since test is a local copy. But how do I make test a
"reference copy" of someT so when I change test I also change someT?
Let your 'test' member be a reference to SomeType, and initialise it
in the initialiser list. The all chagnes you make to 'test' will be
visible in the object referred to by 'someT', as well.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 27 '07 #2
Victor Bazarov wrote:
meekee wrote:
>In class A I get a reference to 'SomeType':

class A {
SomeType test;
public:
A(SomeType& someT) {
test = someT;

Don't assign. Initialise.
>}
..
..
};

But if I do operations on test the are not visible in the someT
instance since test is a local copy. But how do I make test a
"reference copy" of someT so when I change test I also change someT?

Let your 'test' member be a reference to SomeType, and initialise it
in the initialiser list. The all chagnes you make to 'test' will be
visible in the object referred to by 'someT', as well.

V

I have tried making the test member a reference to SomeType:

SomeType& test;

but then I get an error that its forbidden to initialize the field. How
do I initialize a field member with an argument that first available in
the constructor?
Sep 27 '07 #3
On Sep 27, 2:32 pm, meekee <mee...@dd.comwrote:
In class A I get a reference to 'SomeType':

class A {
//SomeType test;

SomeType& test;

>
public:
A(SomeType& someT) {
test = someT;

}
..
..

};

But if I do operations on test the are not visible in the someT instance
since test is a local copy. But how do I make test a "reference copy"
of someT so when I change test I also change someT?

Sep 27 '07 #4
meekee wrote:
Victor Bazarov wrote:
>meekee wrote:
>>In class A I get a reference to 'SomeType':

class A {
SomeType test;
public:
A(SomeType& someT) {
test = someT;

Don't assign. Initialise.
>>}
..
..
};

But if I do operations on test the are not visible in the someT
instance since test is a local copy. But how do I make test a
"reference copy" of someT so when I change test I also change
someT?

Let your 'test' member be a reference to SomeType, and initialise it
in the initialiser list. The all chagnes you make to 'test' will be
visible in the object referred to by 'someT', as well.

V


I have tried making the test member a reference to SomeType:

SomeType& test;

but then I get an error that its forbidden to initialize the field.
WHAT?
How do I initialize a field member with an argument that first
available in the constructor?
Use the constructor initialiser list. Doesn't your C++ textbook
explain how to do that?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 27 '07 #5
Jonathan Lane wrote:
On Sep 27, 2:32 pm, meekee <mee...@dd.comwrote:
>In class A I get a reference to 'SomeType':

class A {
//SomeType test;

SomeType& test;

I have tried that but it gives the error:error: uninitialized reference
member ‘test’
Sep 27 '07 #6
meekee wrote:
Jonathan Lane wrote:
>On Sep 27, 2:32 pm, meekee <mee...@dd.comwrote:
>>In class A I get a reference to 'SomeType':

class A {
//SomeType test;

SomeType& test;


I have tried that but it gives the error:error: uninitialized
reference member ‘test’
The solution: initialise it! The constructor initialiser list
is the only place for that.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 27 '07 #7
meekee wrote:
>

In class A I get a reference to 'SomeType':

class A {
SomeType test;
public:
A(SomeType& someT) {
test = someT;

}
..
..
};

But if I do operations on test the are not visible in the someT instance
since test is a local copy. But how do I make test a "reference copy"
of someT so when I change test I also change someT?
You have two options:

class A {

SomeType & test;

public:

A ( SomeType & someT )
: test ( someT )
{}

};
or

class A {

SomeType * test_ptr;

public:

A ( SomeType & someT )
: test_ptr ( &someT )
{}

};
The first option is more natural and conveys intend more clearly since using
a pointer in C++ usually indicates that you want to allow for null (whereas
in this class, it is an invariant that test_ptr != 0). The pointer version,
however, can be necessary when you need to implement an assignment
operator: a reference cannot be reseated but you can change the pointer.

Best

Kai-Uwe Bux
Sep 27 '07 #8
Victor Bazarov wrote:
meekee wrote:
>Victor Bazarov wrote:
>>meekee wrote:
In class A I get a reference to 'SomeType':

class A {
SomeType test;
public:
A(SomeType& someT) {
test = someT;
Don't assign. Initialise.

}
..
..
};

But if I do operations on test the are not visible in the someT
instance since test is a local copy. But how do I make test a
"reference copy" of someT so when I change test I also change
someT?
Let your 'test' member be a reference to SomeType, and initialise it
in the initialiser list. The all chagnes you make to 'test' will be
visible in the object referred to by 'someT', as well.

V

I have tried making the test member a reference to SomeType:

SomeType& test;

but then I get an error that its forbidden to initialize the field.

WHAT?
>How do I initialize a field member with an argument that first
available in the constructor?

Use the constructor initialiser list. Doesn't your C++ textbook
explain how to do that?

V
Hm it works if I do:
1)
class A {
SomeType& test;
public:
A(SomeType& someT) test(someT) {}
...
...
};
but not if I do:

2)
class A {
SomeType& test;
public:
A(SomeType& someT) {
test=someT
}
...
...
};

So initializing in 1) differs from assigning in 2) as you pointed out,
thanks!
Sep 27 '07 #9
meekee wrote:
Victor Bazarov wrote:
>meekee wrote:
>>Victor Bazarov wrote:
meekee wrote:
In class A I get a reference to 'SomeType':
>
class A {
SomeType test;
>
>
public:
A(SomeType& someT) {
test = someT;
Don't assign. Initialise.

}
..
..
};
>
But if I do operations on test the are not visible in the someT
instance since test is a local copy. But how do I make test a
"reference copy" of someT so when I change test I also change
someT?
Let your 'test' member be a reference to SomeType, and initialise it
in the initialiser list. The all chagnes you make to 'test' will be
visible in the object referred to by 'someT', as well.

V

I have tried making the test member a reference to SomeType:

SomeType& test;

but then I get an error that its forbidden to initialize the field.

WHAT?
>>How do I initialize a field member with an argument that first
available in the constructor?

Use the constructor initialiser list. Doesn't your C++ textbook
explain how to do that?

V

Hm it works if I do:
1)
class A {
SomeType& test;
public:
A(SomeType& someT) test(someT) {}
..
..
};
but not if I do:

2)
class A {
SomeType& test;
public:
A(SomeType& someT) {
test=someT
}
..
..
};

So initializing in 1) differs from assigning in 2) as you pointed out,
thanks!

....the only problem with this is that its no longer possible to have a
default constructor, or am I missing something?
Sep 27 '07 #10
meekee <me****@dd.comwrites:

>...the only problem with this is that its no longer possible to have a
default constructor
True. By chance I've recently done a page which deals with this kind
of stuff - just the basics

http://www-h.eng.cam.ac.uk/help/tpl/...structors.html

Sep 27 '07 #11
meekee <me****@dd.comwrote in news:46***********************@news.sunsite.dk:
>

...the only problem with this is that its no longer possible to have a
default constructor, or am I missing something?
If you want to allow a default constructor, then you need something which can
be uninitialized. In other words a pointer.

joe
Sep 27 '07 #12
Joe Greer wrote:
meekee <me****@dd.comwrote in
news:46***********************@news.sunsite.dk:
>>

...the only problem with this is that its no longer possible to have
a default constructor, or am I missing something?

If you want to allow a default constructor, then you need something
which can be uninitialized. In other words a pointer.
Or initialise the reference to something that can be meaningful for
a that type of initialisation, like a static object or a 'new'd one.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 27 '07 #13
"Victor Bazarov" <v.********@comAcast.netwrote in news:fdgufk$qrg$1
@news.datemas.de:
Joe Greer wrote:
>meekee <me****@dd.comwrote in
news:46***********************@news.sunsite.dk:
>>>

...the only problem with this is that its no longer possible to have
a default constructor, or am I missing something?

If you want to allow a default constructor, then you need something
which can be uninitialized. In other words a pointer.

Or initialise the reference to something that can be meaningful for
a that type of initialisation, like a static object or a 'new'd one.

V
True, but I never have understood pointer phobia. Pointers are perfect for
this situation.

joe
Sep 27 '07 #14
Joe Greer wrote:
"Victor Bazarov" <v.********@comAcast.netwrote in news:fdgufk$qrg$1
@news.datemas.de:
>Joe Greer wrote:
>>meekee <me****@dd.comwrote in
news:46***********************@news.sunsite.dk :

...the only problem with this is that its no longer possible to
have a default constructor, or am I missing something?

If you want to allow a default constructor, then you need something
which can be uninitialized. In other words a pointer.

Or initialise the reference to something that can be meaningful for
a that type of initialisation, like a static object or a 'new'd one.

V

True, but I never have understood pointer phobia. Pointers are
perfect for this situation.
There is no phobia. It's the matter of having to check it every
time before dereferencing. Pointers can be null, references can't.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 27 '07 #15
meekee <me****@dd.comwrote in news:46fbb895$0$90266$14726298
@news.sunsite.dk:
Hm it works if I do:
1)
class A {
SomeType& test;
public:
A(SomeType& someT) test(someT) {}
..
..
};
This is initialization.
but not if I do:

2)
class A {
SomeType& test;
public:
A(SomeType& someT) {
test=someT
}
..
..
};
This is assignment.
So initializing in 1) differs from assigning in 2) as you pointed out,
thanks!
Yep. One is initialization, one is not.
Sep 27 '07 #16
So initializing in 1) differs from assigning in 2) as you pointed out,
thanks!

...the only problem with this is that its no longer possible to have a
default constructor, or am I missing something?
Indeed, references have these properties:
Cannot be null
Must be initialised when created
Cannot be reassigned

That is, a reference is an alias to a pre-existing object not an
object in its own right. Therefore it doesn't make sense to have any
sort of constructor since it's not an object per-se. Making it a
reference and initialising it in the initialisation list is the way to
achieve what you asked for. If you need to be able to reassign this/
delay initialisation then it should be a pointer or some sort of smart
pointer.

Sep 28 '07 #17

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

Similar topics

0
by: Andreas Suurkuusk | last post by:
Hi, I just noticed your post in the "C# memory problem: no end for our problem?" thread. In the post you implied that I do not how the garbage collector works and that I mislead people. Since...
6
by: Rowan | last post by:
Hello, I am having a problem with a join. Either I am just not seeing the obvious, it isn't possible, or I need to use a different approach. I have an application with a vsflexgrid that needs...
3
by: Dave | last post by:
I have an Access 2K application that is distributed to about a dozen users (all with identical NT environments and identical Access versions, object libraries and service packs). I am using the VBA...
15
by: Ken Allen | last post by:
I have been developing a suite of assemblies over the past couple of weeks, and this afternoon somethign started misbehaving. If I do not run the IDE and compiler the code from the command line,...
7
by: George Copeland | last post by:
This is a request for assistance analyzing a problem we are experiencing in our VB6 development environment. All our code is developed in VB6, and our persistance layer is SQL Server. We are...
2
by: Joe Wedel | last post by:
I have a Solution with 4 Projects, one of which is not loading. I get the error message: Microsoft Development Environment Unable to read the project file 'CouncilAgenda.vbproj'. The project...
16
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
26
by: Tom Becker | last post by:
Is there a way, from Access, to programmatically click the Send and Receive button in Outlook?
5
by: Reg (Lincolnshire) | last post by:
Hi, On a machine running Vista I have just converted an A2003 application to A2007, built an MSI package for it and installed it on my kid's PC, which is running Windows XP and A2003. I...
4
by: Adrock952 | last post by:
I am trying to create my tables where if i delete/update a record from one table, all the other tables are affected by deleting/updating any records that reference the original record. For...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.