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; Originally Posted by Elysia Your in quite a swamp there. Hang on to the branch! Don't let yourself sink! i ...

  1. #46
    Registered User
    Join Date
    Aug 2008
    Posts
    188
    Quote Originally Posted by Elysia View Post
    Your in quite a swamp there. Hang on to the branch! Don't let yourself sink!
    i was worried that maybe i'm just full of myself...but it's nice to know i at least have some support. a close friend of mine (also programmer) said the best way to deal with it is to object to any decision if you feel you have a reason, but once the decision has been carried out, to implement it in whatever way was decided. at first i was afraid i could lose my job for being 'rebellious' and 'disobedient' but i figured i can't be fired for trying to and doing good work.

  2. #47
    Registered User
    Join Date
    Aug 2008
    Posts
    188
    Quote Originally Posted by iMalc View Post
    What's more, how are you supposed to implement your own resource handling classes. E.g. to write a smart pointer you must use a smart pointer? Are other resources such as handles not just as important, if not more so, to prevent from being leaked? What's his policy on other types of resources?
    the general rule here is to prefer the stack over the heap. i actually like this idea because usually the stack with allow us to do everything faster and more efficient than a heap (and has the added advantage we don't need to worry about leaking memory....at least i think the stack is safe for memory). and then in the case where we need something in the heap, we use a smart pointer.

    as for resource clean up we have a smart handle class, something like this:

    Code:
    class SmartHandle {
      HANDLE h;
      ~SmartHandle() { ::CloseHandle(h); }
    };
    it's got copy/assignment operators assigned so u can just do:
    SmartHandle h = ::CreateFile(...);

    we let the stack unwinding take care of clean up...and if we need the handle longer, we use a smart pointer.

    the general idea is pretty good and i like it. it's just problematic when i want 1 class to refer to another class. his answer to this dilemma was to use a singleton....on my SOAP client class... :'(

  3. #48
    Registered User
    Join Date
    Aug 2008
    Posts
    188
    ...and when i said what happens if we want a 2nd soap class? he said a 2nd singleton....

    i proceeded to scratch my head...

  4. #49
    Registered User
    Join Date
    Aug 2008
    Posts
    188
    i apologize for my random out of order replies...i think this next one covers everything.

    Quote Originally Posted by cpjust View Post
    Well if you just need a const copy of the object that won't get changed externally, then I would make it a regular variable (no reference or pointer). I thought you wanted to make it a reference because you want your copy to see any changes that were made to the object externally?
    normally this is OK, but for this particular application my object Foo will be create hundreds of thousands of times, so it doesn't make sense (well, i don't think it is) for the contents of Bar to be copied every time for Foo...hence the reference.

    btw thanks for all the helpful replies to this thread.

  5. #50
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,307
    I agree on the "easier to ask for forgiveness than permission" stance.
    However once you've done the later for a particular piece of code, then going against the answer and having to ask for forgiveness afterwards would be a lot harder.

    I think at this stage you'd be best to ask one of your peers at your work what they think. Democracies are all good.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #51
    Registered User
    Join Date
    Oct 2006
    Posts
    2,457
    Quote Originally Posted by bling View Post
    - i do NOT want to be able to change the data in bar, hence const
    there's no need for the member to be const. to avoid changing it, you just have to declare all of your member functions (not ctor or dtor functions) as const. then you can have a copy constructor and an assignment operator with no issues.

    - 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...
    I think you're right about that. you can't reassign a const reference (or any reference for that matter) once it's set.

  7. #52
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,246
    This is why you don't try to stick an object which holds a reference into a container.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Page 4 of 4 FirstFirst 1234
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