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

  1. #31
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    That isn't exactly how it works... You are assigning it the value of tmp. Thus it is copying the data over.

  2. #32
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by bling View Post
    ok, 1 final question:

    Code:
    Foo operator = (const Foo& foo) {
      Foo tmp(foo.bar);
      return tmp;
    }
    
    int main() {
      Foo a;
      Foo b;
      a = b;
    }
    when the assignment operator is called, 'a' will fall off the stack and its new value will be 'tmp' right?
    Yes, at least sort of.
    And I've just figured out that with the copy constructor I posted earlier, this copy operator code works.

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

  3. #33
    Registered User
    Join Date
    Aug 2008
    Posts
    188
    @phantomotap
    i just got this job and my opinions don't mean anything no matter how correct it is from an objective point of view. i need to prove myself in other ways first.

  4. #34
    Registered User
    Join Date
    Aug 2008
    Posts
    188
    @Elysia
    again, i don't want to be that vocal this early in my job. however, my supervisor is also the team lead and he does a lot of coding as well, so i can't use that argument....because again, it implies he can't do his job.

    my girlfriend tells me that i'm getting paid to do as he says...so i should just do it, but the programmer in me always wants to find the best way of doing things.

  5. #35
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You wouldn't have been hired if the company and your peers did not value your input.

    Just use a smart pointer and document the change. If your boss gets ........ed then you'll just have to explain.

  6. #36
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Oh and phantomotap, I love your examples. They're always so complex
    I don't know if you are serious or not, but I almost made it meta-programming style. ^_^

    i just got this job and my opinions don't mean anything
    -_-

    You can't know that until you present your case. If you really are new to the job scene/new to the house they will not fire you for the attempt at simplifying your job and improving your work. If your team really doesn't value your input... you are screwed anyway.

    Soma

  7. #37
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Redundancy is always good. Redundancy is always good. Redundancy is always good. Redundancy is always good. Redundancy is always good. Redundancy is always good. Redundancy is always good. Redundancy is always good. Redundancy is always good. Redundancy is always good. Redundancy is always good. Redundancy is always good. Redundancy is always good. Redundancy is always good. Redundancy is always good. Redundancy is always good. Redundancy is always good. Redundancy is always good. Redundancy is always good.

  8. #38
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by bling View Post
    however, my supervisor is also the team lead and he does a lot of coding as well, so i can't use that argument....because again, it implies he can't do his job.
    Then ask your boss what HE thinks is the best solution. If he scratches his head and says idunno, then tell him you're using pointers until he can come up with his own solution.
    Either that or ask him if you can re-write the whole thing in Java or C#.

  9. #39
    Registered User
    Join Date
    Aug 2008
    Posts
    188
    Quote Originally Posted by cpjust View Post
    Then ask your boss what HE thinks is the best solution. If he scratches his head and says idunno, then tell him you're using pointers until he can come up with his own solution.
    Either that or ask him if you can re-write the whole thing in Java or C#.
    i did. he said don't worry about redundancy and just copy the data over and over again. i had a very strong opinion not to do this and said that if that data is already in memory somewhere else and is not going to change, i should reuse that. he thought that i was making life difficult for myself. i said i can handle it and i believe that it's a good design. i eventually let me use the reference (pointer is still a no-no) and said that it'll be open to review and i may have to change everything if he doesn't like it.

    the good news is that my current solution will compile with or without the '&' which means i can turn on/off copying quite easily.

    as for C# i'm very adept in that language because that was what i used for 3 years at my previous job. it would actually make a lot of sense to use C# for my application because we're much more concerned with 'safety' than performance....even though when i was hired the manager said they still use C++ because of performance.

    i've grown to like C++ again, like RAII, smart pointers, etc. it's all very interesting and cool, although i hate the idea that i'm not allowed to use the language's most powerful features.
    Last edited by bling; 09-05-2008 at 10:39 PM.

  10. #40
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    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?

  11. #41
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You need to chat with your boss about the reasons for not using raw pointers. Almost certainly it's because of the possibility of forgetting to delete something and causing a memory leak.
    That being the case, using a reference instead does not lessen the possibility of getting a memory leak. Furthurmore you actually just make things somewhere between difficult and impossible to do what you need to do. So don't do this.

    Here's something you need to remind your boss. Weak/raw pointers are absolutely necessary. For example, when two classes have a ref-counted smart pointer to each other. Neither can be freed. The solution to such problems is to use a weak/raw pointer in one of them. If you can't do that then his rules are causing the very problem he is trying to prevent - leaking. Seriously you need to give him the circular references counter-example.

    What about pointers to things the object doesn't own? Holding smart pointers to such items would be ridiculous as you'd have to explicitly detach from the smart pointer to prevent from deleting something you're not supposed to. However this is the case where references can be the right thing to use, and even then I quite probably wouldn't.

    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?

    I am all for code reviews. We use them heavily where I work. They're a much better way of improving code quality than making arbitrary rules.
    Last edited by iMalc; 09-06-2008 at 01:15 AM.
    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"

  12. #42
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    Quote Originally Posted by citizen View Post
    Just use a smart pointer and document the change. If your boss gets ........ed then you'll just have to explain.
    Agreed. Often, it is easier to beg forgiveness than to ask permission. Especially, as in this case, when you are actually doing the right thing by going against the rule.

  13. #43
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by phantomotap View Post
    I don't know if you are serious or not, but I almost made it meta-programming style. ^_^
    I'm pretty serious, but I also like when you chime in your examples ^_^
    They look nice.

    Quote Originally Posted by bling View Post
    the good news is that my current solution will compile with or without the '&' which means i can turn on/off copying quite easily.
    You are aware that if you use a reference, then you CAN'T use the assignment operator because a reference can't be reassigned, right?
    Although without it, there's no problems.
    I'm just checking to make sure you're aware.
    Your in quite a swamp there. Hang on to the branch! Don't let yourself sink!
    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.

  14. #44
    Registered User
    Join Date
    Aug 2008
    Posts
    188
    Quote Originally Posted by iMalc View Post
    Seriously you need to give him the circular references counter-example.
    my first project's design involved a (half) circular reference. a class derived from a threading class that we use, which implemented a run() function. i needed to add a 2nd thread to the same class, so i made an inner class implement the same base thread class, and passed in a pointer of the base class to the inner class. so it was just one way, the inner class held a pointer to the main class so i could access privates.

    the smart pointer implementation we use threw a hissy fit about this (because the inner class would try to delete something that still should exist) so i pointed that out to my supervisor, mentioning that i wasn't able to use a smart pointer in this case. (i've mentioned using boost many times but his reply was to keep using their in-bread library). he replied saying isn't using circular references a bad design??

    i tried to convince him that it's useful...for instance in the .NET framework u'd have stuff like:
    Code:
    class B {
      int a,b,c;
    };
    class A {
      B* b;
    };
    where u can create a hierarchy of information to make it easier to understand. he said i should forget about my C# practices because i'm programming in C++ now. i didn't exactly agree with this statement and probably shoulda used a C++ example....i wasn't sure what to say to that so i just accepted what he said because i felt it was derailing into a battle of egos.
    Last edited by bling; 09-06-2008 at 01:07 PM.

  15. #45
    Registered User
    Join Date
    Aug 2008
    Posts
    188
    Quote Originally Posted by Elysia View Post
    You are aware that if you use a reference, then you CAN'T use the assignment operator because a reference can't be reassigned, right?
    Although without it, there's no problems.
    I'm just checking to make sure you're aware.
    Your in quite a swamp there. Hang on to the branch! Don't let yourself sink!
    yes, that's correct. that's why the operator= creates temporary instance, and returns a copy, which replaces the original. i'm undecided whether i want to put in a 'throw "you're not supposed to use this"' in there.

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, 12:52 PM
  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, 01: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, 12:54 PM