Passing an Object to Another Object

H

Hal Vaughan

If I have object A, B, and C, all from separate classes and I create A, then
inside A, I create B, then I want to pass B to C, I know if I do
that "normally" what I get in C is actually a copy of B, not the original,
so if C modifies something in its copy of B, it doesn't effect A's copy of
B.

I've been experimenting using a reference and a pointer so I can pass B from
A to C and both will end up using the same copy of B, but I can't get it to
work. I'm sure this is so simple I'll feel like a complete idiot when I'm
done, but I just can't get it to work.

I've tried using this in C:

ObjB objb;

void setobj(ObjB* obj) {
objb = *obj;
return;
}

I've also tried replacing the first line of the function as the same, but
without an asterisk. I'm calling the function in A like this:

objc.setobj(&objb);

I would think that I'm passing a reference to objb from A to C, then all I'd
have to do is tell C that its objb is where the pointer points, but I can't
get it to work. I need for both A and C to be using the same instance of
B.

What am I doing wrong and what do I need to do instead?

Thanks!

Hal
 
M

Martin York

If I have object A, B, and C, all from separate classes and I create A, then
inside A, I create B, then I want to pass B to C, I know if I do
that "normally" what I get in C is actually a copy of B, not the original,
so if C modifies something in its copy of B, it doesn't effect A's copy of
B.

I've been experimenting using a reference and a pointer so I can pass B from
A to C and both will end up using the same copy of B, but I can't get it to
work. I'm sure this is so simple I'll feel like a complete idiot when I'm
done, but I just can't get it to work.

I've tried using this in C:

ObjB objb;

void setobj(ObjB* obj) {
objb = *obj;
return;

}

I've also tried replacing the first line of the function as the same, but
without an asterisk. I'm calling the function in A like this:

objc.setobj(&objb);

I would think that I'm passing a reference to objb from A to C, then all I'd
have to do is tell C that its objb is where the pointer points, but I can't
get it to work. I need for both A and C to be using the same instance of
B.

What am I doing wrong and what do I need to do instead?

Thanks!

Hal

Close:

class C
{

ObjB* objb;

public:
void setobj(ObjB* obj)
{
objb = obj;
}
};
 
H

Hal Vaughan

Martin said:
Close:

class C
{

ObjB* objb;

public:
void setobj(ObjB* obj)
{
objb = obj;
}
};

Then can I access functions in objb like objb.func(), or do I have to do it
like objb->func()?

Thanks! It's good to know I wasn't far off!

Hal
 
H

Hal Vaughan

Hal said:
Martin York wrote: ....

Then can I access functions in objb like objb.func(), or do I have to do
it like objb->func()?

Okay, it wouldn't compile unless I changed any usage of it in C from
objb.func() to objb->func(). I can work with that fine, but for my
understanding and general academic curiosity, is there a way to actually
reference it in C as an object and not as a pointer to the object?

Hal
 
J

James Connell

Hal said:
Okay, it wouldn't compile unless I changed any usage of it in C from
objb.func() to objb->func(). I can work with that fine, but for my
understanding and general academic curiosity, is there a way to actually
reference it in C as an object and not as a pointer to the object?

Hal

yes, pass it by reference.
class C
{

ObjB objb;

public:
void setobj(ObjB& obj)
{
objb = obj;
}
};
 
H

Hal Vaughan

James said:
yes, pass it by reference.
class C
{

ObjB objb;

public:
void setobj(ObjB& obj)
{
objb = obj;
}
};

Ah! Even easier, all around!

Thanks!

Hal
 
K

Kai-Uwe Bux

Not if you don't want a copy. You have to use ObjB* since an ObjB& cannot be
reseated and therefore not be changed in setobj.


yes, pass it by reference.
class C
{

ObjB objb;

public:
void setobj(ObjB& obj)
{
objb = obj;
}
};

With that code, objb is still a copy of obj.


Best

Kai-Uwe Bux
 
Y

Yannick Tremblay

Not if you don't want a copy. You have to use ObjB* since an ObjB& cannot be
reseated and therefore not be changed in setobj.




With that code, objb is still a copy of obj.

References cannot be reseated. The must be initialised at creation
time. But then, assuming that both A and C must use B for something.
It probably doesn't make sense for C to exists without having access
(reference) to a B. Hence, it might very well be better to set it in
the constructor and there you can use a reference:

Class C
{
public:
explicit C(ObjB & objb) : m_objb(objb) {} ;
private:
ObjB & m_objb;
}

Advantages:
- C always have a B reference.

Disadvantage:
- The C reference to a B can't be changed after creation time.

Yan
 
H

Hal Vaughan

Yannick said:
Not if you don't want a copy. You have to use ObjB* since an ObjB& cannot
be reseated and therefore not be changed in setobj.




With that code, objb is still a copy of obj.

References cannot be reseated. The must be initialised at creation
time. But then, assuming that both A and C must use B for something.
It probably doesn't make sense for C to exists without having access
(reference) to a B. Hence, it might very well be better to set it in
the constructor and there you can use a reference:

Class C
{
public:
explicit C(ObjB & objb) : m_objb(objb) {} ;
private:
ObjB & m_objb;
}

Advantages:
- C always have a B reference.

Disadvantage:
- The C reference to a B can't be changed after creation time.

Yan[/QUOTE]

I'd like to do it that way, but I have to declare C in a header and there
seems to be a problem with doing that with an object that requires
arguments in the constructor. I'm sure there's a way around this, but I
haven't gotten there yet.

I'm finding that although a lot of C++ is easy to learn on my own, there are
a lot of details about things like managing headers and the compiler (and
make files) that are often only partially covered in tutorials and other
sources. For instance, I found a number of sources telling me how to
define objects, but not one gave me the format to use if I'm putting it in
a header and including prototype definitions within it. Nothing told me to
put a semicolon after the closing "}" and in most cases in C++ when you
close a function or namespace or anything else with "}" there's no need for
a semicolon, but I did need it in the .h file for defining a class.

Hal
 
M

mohi

I'd like to do it that way, but I have to declare C in a header and there
seems to be a problem with doing that with an object that requires
arguments in the constructor. I'm sure there's a way around this, but I
haven't gotten there yet.

I'm finding that although a lot of C++ is easy to learn on my own, there are
a lot of details about things like managing headers and the compiler (and
make files) that are often only partially covered in tutorials and other
sources. For instance, I found a number of sources telling me how to
define objects, but not one gave me the format to use if I'm putting it in
a header and including prototype definitions within it. Nothing told me to
put a semicolon after the closing "}" and in most cases in C++ when you
close a function or namespace or anything else with "}" there's no need for
a semicolon, but I did need it in the .h file for defining a class.

Hal

if u finding these problems so i will suggest use the book "deitel:how
to program in c++"
i think thats the best book i ever read on c++.
dont think me a sales person i am student and im suggesting it to u
only because i found it that way and presently i am also using it .
mohan
 
J

James Connell

Kai-Uwe Bux said:
With that code, objb is still a copy of obj.


Best

Kai-Uwe Bux

Congratulations your ability to point out the obvious is simply amazing
(or at least simple).
 
K

Kai-Uwe Bux

James said:
Congratulations your ability to point out the obvious is simply amazing
(or at least simple).

Thanks, I feel flattered.

However, in the context you snipped away, the OP explicitly stated that he
does not want to store a copy. He was looking for a way to manipulate the
original ObjbB object from within the C object:

From the OP:
If I have object A, B, and C, all from separate classes and I create A,
then inside A, I create B, then I want to pass B to C, I know if I do
that "normally" what I get in C is actually a copy of B, not the original,
so if C modifies something in its copy of B, it doesn't effect A's copy of
B.

I've been experimenting using a reference and a pointer so I can pass B
from A to C and both will end up using the same copy of B, but I can't get
it to work. ...

That is the only reason, I felt the need to point out the obvious.


Best

Kai-Uwe Bux
 
J

James Connell

Kai-Uwe Bux said:
Thanks, I feel flattered.

Your welcome
I've been experimenting using a reference and a pointer so I can pass B
from A to C and both will end up using the same copy of B, but I can't get
it to work. ...

That is the only reason, I felt the need to point out the obvious.

As usual y'all over complicated the problem - the OP was trying figure
out a way to pass a *reference* that was all. The code you had a problem
with was simply that, pass a reference to an object. What the function
did with that reference is immaterial. I simply used a code fragment the
OP had already seen ( and analyzed, I hope) in the post immediately
previous.

when learning C++, one of the hardest operators in C++ to get your head
around is the 'reference op' it seems simple - &x is a reference, except
it isn't.

Very few people new to the language pick out that a reference sent to a
function has the same look as any other variable. It's in the
declaration/definition of the function the the reference instead of a
'by value' param is specified. ( I'm aware that you know all this BUT
the OP doesn't/didn't )
 

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,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top