Passing an Object to Another Object

Discussion in 'C++' started by Hal Vaughan, Mar 27, 2008.

  1. Hal Vaughan

    Hal Vaughan Guest

    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
     
    Hal Vaughan, Mar 27, 2008
    #1
    1. Advertising

  2. Hal Vaughan

    Martin York Guest

    On Mar 26, 8:25 pm, Hal Vaughan <> wrote:
    > 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;
    }
    };
     
    Martin York, Mar 27, 2008
    #2
    1. Advertising

  3. Hal Vaughan

    Hal Vaughan Guest

    Martin York wrote:

    > On Mar 26, 8:25 pm, Hal Vaughan <> wrote:
    >> 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;
    > }
    > };


    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
     
    Hal Vaughan, Mar 27, 2008
    #3
  4. Hal Vaughan

    Hal Vaughan Guest

    Hal Vaughan wrote:

    > Martin York wrote:

    ....
    >>
    >> 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()?


    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
     
    Hal Vaughan, Mar 27, 2008
    #4
  5. Hal Vaughan wrote:
    > Hal Vaughan wrote:
    >
    >> Martin York wrote:

    > ...
    >>> 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()?

    >
    > 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;
    }
    };
     
    James Connell, Mar 27, 2008
    #5
  6. Hal Vaughan

    Hal Vaughan Guest

    James Connell wrote:

    > Hal Vaughan wrote:
    >> Hal Vaughan wrote:
    >>
    >>> Martin York wrote:

    >> ...
    >>>> 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()?

    >>
    >> 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;
    > }
    > };


    Ah! Even easier, all around!

    Thanks!

    Hal
     
    Hal Vaughan, Mar 27, 2008
    #6
  7. Hal Vaughan

    Kai-Uwe Bux Guest

    James Connell wrote:

    > Hal Vaughan wrote:
    >> Hal Vaughan wrote:
    >>
    >>> Martin York wrote:

    >> ...
    >>>> 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()?

    >>
    >> 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?


    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.



    >>
    >> Hal

    >
    > 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
     
    Kai-Uwe Bux, Mar 27, 2008
    #7
  8. In article <fsfcs2$19b$>, Kai-Uwe Bux <> wrote:
    >James Connell wrote:
    >
    >> Hal Vaughan wrote:
    >>> Hal Vaughan wrote:
    >>>
    >>>> Martin York wrote:
    >>> ...
    >>>>> 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()?
    >>>
    >>> 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?

    >
    >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.
    >
    >
    >
    >>>
    >>> Hal

    >>
    >> 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.


    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
     
    Yannick Tremblay, Mar 27, 2008
    #8
  9. Hal Vaughan

    Hal Vaughan Guest

    Yannick Tremblay wrote:

    > In article <fsfcs2$19b$>, Kai-Uwe Bux <>
    > wrote:
    >>James Connell wrote:
    >>
    >>> Hal Vaughan wrote:
    >>>> Hal Vaughan wrote:
    >>>>
    >>>>> Martin York wrote:
    >>>> ...
    >>>>>> 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()?
    >>>>
    >>>> 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?

    >>
    >>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.
    >>
    >>
    >>
    >>>>
    >>>> Hal
    >>>
    >>> 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.

    >
    > 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


    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
     
    Hal Vaughan, Mar 27, 2008
    #9
  10. Hal Vaughan

    mohi Guest

    On Mar 27, 9:26 pm, Hal Vaughan <> wrote:
    > Yannick Tremblay wrote:
    > > In article <fsfcs2$>, Kai-Uwe Bux <>
    > > wrote:
    > >>James Connell wrote:

    >
    > >>> Hal Vaughan wrote:
    > >>>> Hal Vaughan wrote:

    >
    > >>>>> Martin York wrote:
    > >>>> ...
    > >>>>>> 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()?

    >
    > >>>> 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?

    >
    > >>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.

    >
    > >>>> Hal

    >
    > >>> 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.

    >
    > > 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

    >
    > 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
     
    mohi, Mar 27, 2008
    #10
  11. Kai-Uwe Bux wrote:

    >> 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


    Congratulations your ability to point out the obvious is simply amazing
    (or at least simple).
     
    James Connell, Mar 27, 2008
    #11
  12. Hal Vaughan

    Kai-Uwe Bux Guest

    James Connell wrote:

    > Kai-Uwe Bux wrote:
    >
    >>> 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

    >
    > 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
     
    Kai-Uwe Bux, Mar 28, 2008
    #12
  13. Kai-Uwe Bux wrote:

    >
    > 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 )
     
    James Connell, Mar 28, 2008
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. csvka
    Replies:
    6
    Views:
    14,514
    John Harrison
    Feb 16, 2004
  2. Phillip Vong
    Replies:
    0
    Views:
    433
    Phillip Vong
    Jul 27, 2006
  3. Replies:
    14
    Views:
    646
    RichardETVS
    May 29, 2007
  4. Robert Cohen
    Replies:
    3
    Views:
    305
    Andrew Durstewitz
    Jul 15, 2003
  5. Davide Bruzzone
    Replies:
    2
    Views:
    266
    Davide Bruzzone
    Aug 4, 2003
Loading...

Share This Page