copy constructor for a class with a const reference member...

This is a discussion on copy constructor for a class with a const reference member... within the C++ Programming forums, part of the General Programming Boards category; hello, i'm struggling to come up with the correct syntax. Code: class Foo { public: Foo(const Bar& bar); private: const ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    188

    copy constructor for a class with a const reference member...

    hello, i'm struggling to come up with the correct syntax.

    Code:
    class Foo
    {
    public:
      Foo(const Bar& bar);
    
    private:
      const Bar& m_bar;
    };
    
    
    Foo::Foo(const Bar& bar)
      : m_bar(bar)
    { }
    that works and compiles...until i put Foo into a vector, and it complains i don't have a assignment operator.

    ok, so i define one...but now i have a problem where i don't want to change the m_bar reference because i can't assign it something else...

    is there a way around this?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    When you do
    Code:
    Foo a(whatever);
    Foo b = a;
    what reference do you want b to have? (If the answer is not "whatever", why not?) (If the answer is "I want a separate copy", then we shouldn't even have references in the first place.)

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Couldn't you just use a pointer instead of a reference?

  4. #4
    Registered User
    Join Date
    Aug 2008
    Posts
    188
    at my job my supervisor forbids me to use any pointers directly (must use a smart pointer if required), so a reference is the only acceptable compromise.

    i can get it to work if i remove the constness, but i want to keep that.

    @tabstop,
    Foo a(whatever);
    Foo b = a; // yes, b should contain the values of a, including the reference to whatever

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I wonder why the guideline against pointers... Sometimes I have found one simple work around that doesn't even require any special programming knowledge is simply "Boss, I need this ctor to utilize a pointer, is that ok?"

    You never know what (s)he just might agree to allow.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by bling View Post
    at my job my supervisor forbids me to use any pointers directly (must use a smart pointer if required), so a reference is the only acceptable compromise.
    Well that's a stupid rule... I'm guessing your supervisor is a Java programmer who is affraid of the evil pointers in C/C++?

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    So use a smart pointer. It's perfect for the job.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    Well foo is not assignable by design. Let foo have a bar shared pointer, or just bar, if you can avoid the indirection.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Then your copy constructor should set m_bar of the new thing to m_bar of the old thing. Somehow I'm missing the problem? (Assignment operator for an existing left-hand side can't work with a reference object, since references can't be re-seated, so I'm hoping you don't mean assignment operator.)

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by cpjust View Post
    Well that's a stupid rule... I'm guessing your supervisor is a Java programmer who is affraid of the evil pointers in C/C++?
    Sometimes its actually not that at all. The primary reason pointers are even allowed in C++ is for compatibility with C. Even Stroustrup has been known to disagree with the way they can skirt some of the rules of encapsulation.

  11. #11
    Registered User
    Join Date
    Aug 2008
    Posts
    188
    surprisingly he has no background in java/c# that i know of. his reasoning is to avoid memory leaks, but maybe he doesn't know that * doesn't necessarily mean it's on the heap. i'm still on probation so i can't do too much.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm pretty sure you CAN do this:
    Code:
    class Foo
    {
    public:
        .... 
        Foo(const Foo& foo);
        Foo operator=(const Foo& foo);
    ...
    };
    
    
    Foo::Foo(const Foo &foo)
    {
       *this = foo;
    }
    
    Foo Foo::Operator=(const Foo &foo)
    {
          Foo tmp(foo.bar);
         // Copy other stuff from foo to tmp.
         return tmp;
    }

    It's a bit convoluted, but that is what you get from having stupid rules like "you can't use pointers". Can your manager explain how you would create a linked list without pointers? [And no, using "std::list" is not a valid answer here]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    *scratches head* Well I had actually started making a mental list of good reasons to support this rule, just in case one of you guys started the typical chain of arguing... However, I would have never guessed that this is the reason behind the rule. Odd indeed.

    Check this out:

    Memory Leak:
    Code:
    myclass &getClass(int i)
    {
         return *(new myclass(i));
    }
    ...But whatever.

  14. #14
    Registered User
    Join Date
    Aug 2008
    Posts
    188
    anyways, the problem is that my reference is const...so i can't reseat, so i can't make an assignment operator nor a copy constructor...

    here's what i need:
    - i do NOT want to be able to change the data in bar, hence const
    - but i DO want to be able to redirect it to something else, which i'm guess the const and no-reseating for references is messing me up...

  15. #15
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Have you tried the asking the boss for permission thing just yet? I was being completely serious when I suggested that.

Page 1 of 4 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Immediate programming help! Please!
    By xMEGANx in forum C++ Programming
    Replies: 6
    Last Post: 02-20-2008, 11:52 AM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 12:26 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 11:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21