Thread: Checking Object's class.

  1. #16
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by whiteflags View Post
    It's called the named constructor idiom.
    As I thought, someone would give a quick answer...

    In addition, to prevent inheritance, the actual constructor would have to be private, right?

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

  2. #17
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Yes, as is implied by the page I linked. Anyway, it's static methods returning objects of your class.

    I still find it a bit confusing, since even though there is no default constructor in the linked example, you can make a stack object. And if you can make a stack object using a (magic) default constructor like that, I don't see how that prevents inheritance.

  3. #18
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by whiteflags View Post
    Yes, as is implied by the page I linked. Anyway, it's static methods returning objects of your class.

    I still find it a bit confusing, since even though there is no default constructor in the linked example, you can make a stack object. And if you can make a stack object using a (magic) default constructor like that, I don't see how that prevents inheritance.
    Good point. You'd have to make the copy constructor protected/private, which would severely limit what you could do with the object (couldn't be stored in a vector, for instance).

  4. #19
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I wonder if C++0x fixes this because otherwise inheritance in C++ is poorly conceived.

  5. #20
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by whiteflags View Post
    I wonder if C++0x fixes this because otherwise inheritance in C++ is poorly conceived.
    I don't see how it could, honestly. Even so, to the contrary I would say that C++ has a pretty good inheritance scheme (much better than Java or C#, for sure).

  6. #21
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I can't talk about other systems, but making sure you don't have derived objects sure is a song and dance in C++. I just wish there were a keyword or something so it's completely clear what the intentions are of the person writing the class.

  7. #22
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by whiteflags View Post
    I can't talk about other systems, but making sure you don't have derived objects sure is a song and dance in C++. I just wish there were a keyword or something so it's completely clear what the intentions are of the person writing the class.
    On second thought, I'm going to agree with you there. It might be useful in some cases. And now that I think about it, I suppose it wouldn't be so hard to implement, after all.

  8. #23
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    Sorry Elysia, you're 100% right, it seems you're experienced in this case.


    Yes, it's expensive to do that, otherwise it will be complicated (creating your own programming language or creating sub-preproccessor just like Qt's moc).

    I think I'll give up on this and just use only what I needed to solve things.

    Thank you very much.
    Just GET it OFF out my mind!!

  9. #24
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Sebastiani
    You'd have to make the copy constructor protected/private, which would severely limit what you could do with the object (couldn't be stored in a vector, for instance).
    Stroustrup provides another "song and dance" solution that does not have such a limitation in his answer to the FAQ Can I stop people deriving from my class?.

    Personally, unless documented otherwise, I stick to the rule that a class with no virtual functions should not be used as a base class. But yeah, having the rule enforced by a language feature would probably be good.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #25
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by laserlight View Post
    Stroustrup provides another "song and dance" solution that does not have such a limitation in his answer to the FAQ Can I stop people deriving from my class?.

    Personally, unless documented otherwise, I stick to the rule that a class with no virtual functions should not be used as a base class. But yeah, having the rule enforced by a language feature would probably be good.
    Interesting. That's a very clean solution, actually. Thanks for that out, Laserlight.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. class composition constructor question...
    By andrea72 in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2008, 05:11 PM
  2. forbid creating copies of objects of child classes
    By sigi in forum C++ Programming
    Replies: 3
    Last Post: 03-03-2008, 09:17 AM
  3. Constructing Base Class Objects
    By Angelina in forum C++ Programming
    Replies: 3
    Last Post: 01-15-2008, 11:28 AM
  4. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  5. Need help to build network class
    By weeb0 in forum C++ Programming
    Replies: 0
    Last Post: 02-01-2006, 11:33 AM