CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 4 1234 LastLast
Results 1 to 15 of 64

Hybrid View

  1. #1
    Join Date
    Dec 2010
    Posts
    907

    Abstraction concept problem?

    When I design my application, I always want to group up everything in common
    to the base class, and have other classes which have that component
    to get a pointer which is the one that is as abstract as possible.
    But in some of my applications, I use boost::shared_ptr a lot,
    When I put the component as abstract as possible, when it goes to
    implementation, I always want to do a boost:ynamic_point_cast
    to the concrete class, which is a downcast.
    Is it recommended? or is it a good coding practice?
    I sometimes, or maybe confused, that upcast is the common practice where downcast is not recommended?
    I find some of my pointers are not allowed to be converted by the compiler?
    Thanks
    Jack

  2. #2
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Abstraction concept problem?

    if you are finding yourself to be using a lot of pointers in designing interfaces... you're probably doing something wrong somewhere.

    If abstraction is causing you to introduce pointers, you should be looking at inheritance instead.

  3. #3
    Join Date
    Mar 2001
    Posts
    2,529

    Re: Abstraction concept problem?

    I met a man who said pointers were evil and did everything he could to avoid them. I don't go that far, but I think you should only use a pointer where it is really necessary.

    It is cumbersome that even with "built-in" polymorphism one should have to use a cast at all.
    ahoodin
    To keep the plot moving, that's why.

  4. #4
    Join Date
    Jun 2015
    Posts
    208

    Re: Abstraction concept problem?

    Quote Originally Posted by ahoodin View Post
    I met a man who said pointers were evil and did everything he could to avoid them.
    Did he explain why?

    What about polymorphic designs (like in this thread)?

    What about the slicing problem?
    Last edited by tiliavirga; July 1st, 2015 at 02:43 AM.

  5. #5
    Join Date
    Mar 2001
    Posts
    2,529

    Re: Abstraction concept problem?

    Quote Originally Posted by tiliavirga View Post
    Did he explain why?
    Yep. He is religious and said they were a tool of the devil.
    He also went on to complain about a lot of misuse and abuse
    leading to strange crashes after code with many pointers
    was edited by other software engineers.

    Quote Originally Posted by tiliavirga View Post
    What about polymorphic designs (like in this thread)?
    Uh yeah that was my point, so I will claim that one.
    Quote Originally Posted by tiliavirga View Post
    What about the slicing problem?
    same thing.

    Pointers are among the most misused and abused constructs
    in C and C++. You may not be among the list of abusers, but
    consider the context of a large project where you have a range
    of skills working on it. Say 50% don't really have a good knowledge
    of pointers but try to do it anyway. What do you get?

    Memory Exceptions, Segmentation Faults, funky behavior, problems, trouble.
    Last edited by ahoodin; July 1st, 2015 at 11:28 AM. Reason: Memory Exceptions added to list
    ahoodin
    To keep the plot moving, that's why.

  6. #6
    Join Date
    Jun 2015
    Posts
    208

    Re: Abstraction concept problem?

    Quote Originally Posted by ahoodin View Post
    Pointers are among the most misused and abused constructs
    in C and C++. You may not be among the list of abusers, but
    consider the context of a large project where you have a range
    of skills working on it. Say 50% don't really have a good knowledge
    of pointers but try to do it anyway. What do you get?

    Memory Exceptions, Segmentation Faults, funky behavior, problems, trouble.
    I agree. You are using the same argument James Gosling used some 20 years ago when he declared C++ unsafe and introduced Java (which later spawned C#). But remember he did never discard pointers! They're live and well in both Java and C# only not in raw form but dressed up in a nice clothing.

    So should C++ programmers refrain from pointers altogether while their Java and C# colleagues get to enjoy them everyday? I don't think so but I recognice that C++ is different and requires a different approach. C++ is a versatile systems programming language which allows you to do things a Java or C# programmer can only dream about. That's both a curse and a blessing and the trick to deal with this double-edged sword is to apply self-control. C++ programmers must learn to restrict themselves to safe and sound practices. If they cannot do that they're better off using Java or C# or some other language where good behaviour is imposed upon them rather than a matter of choise.

    Fortunately in recent years C++ has evolved to the point (read versons 11 and 14) where it is as safe as Java and C#. And safe pointer usage is straightforward: Prefer smart pointers over raw pointers, prefer iterators over pointer arithmetics and prefer implicit iteration constructs over iterators. Simple as that.

    Pointers are an essential part of C++. Banning them is not an option. Instead use them safely by graduating to modern C++. If you are unable to make that leap consider Java or C#. C++ is not for everyone.

    same thing.
    You are either underestimating or not aware of the slicing problem in C++. Slicing is a consequence of not holding polymorphic objects by pointer. If you don't you will sooner or later copy such objects by value which will cut them to pieces.

    in other words, by banning pointers because they're unsafe you are opening the door for another equally severe or even worse safety issue - object slicing. So be smart and hold polymorphic objects by smart pointer.
    Last edited by tiliavirga; July 2nd, 2015 at 03:15 AM.

  7. #7
    Join Date
    Oct 2008
    Posts
    1,456

    Re: Abstraction concept problem?

    Quote Originally Posted by tiliavirga View Post
    Pointers are an essential part of C++. Banning them is not an option. Instead use them safely by graduating to modern C++. If you are unable to make that leap consider Java or C#. C++ is not for everyone.
    historically, pointers mostly meant "mutable references"; nowadays, (smart)pointers should strictly means "ownership". An interface that use smart pointers to pass ownership of dependencies around is ok of course and fundamental for any c++ OOP design;
    an interface that uses pointers (smart or dumb) where simple composition/references/etc... can be used are very bad IMO.

    Quote Originally Posted by tiliavirga View Post
    You are either underestimating or not aware of the slicing problem in C++. Slicing is a consequence of not holding polymorphic objects by pointer. If you don't you will sooner or later copy such objects by value which will cut them to pieces.
    aren't you overestimating it ? slicing can be prevented by properly defning copy/assign/move ctor/operators, something that people often do anyway with polymorphic types, and it's very easy and expressive nowadays ( c++11's "= delete/default" ) .

  8. #8
    Join Date
    Mar 2001
    Posts
    2,529

    Re: Abstraction concept problem?

    Quote Originally Posted by tiliavirga View Post
    But remember he did never discard pointers! They're live and well in both Java and C# only not in raw form but dressed up in a nice clothing.
    A. Pointers don't wear clothes.

    Quote Originally Posted by tiliavirga View Post
    in other words, by banning pointers because they're unsafe you are opening the door for another equally severe or even worse safety issue - object slicing.
    B. Nobody is talking about banning pointers or you didn't read. We are saying that they should only be used where necessary, not just because you can.

    C. I can't think of anything else.. The guys got all the other points.
    Last edited by ahoodin; July 6th, 2015 at 09:03 AM. Reason: sentence structure
    ahoodin
    To keep the plot moving, that's why.

  9. #9
    Join Date
    Jun 2015
    Posts
    208

    Re: Abstraction concept problem?

    Quote Originally Posted by lucky6969b View Post
    upcast is the common practice where downcast is not recommended?
    One reason is type safety. An upcast can be fully checked at compiletime and so will never fail a runtime. That's why you don't have to cast explictly in that case if you don't want to. A downcast on the other hand may fail at runtime with disastrous results. That's why the compiler forces you to take on full responsibility for any dire consequences by insisting on an explicit cast so you cannot claim you weren't aware it could turn bad.

    Another reason is good design. Downcasting is the very opposite of abstraction. You first spend a lot of time writing general easy to extend code using abstract classes. Then you ruin the whole effort by tying down your code to concrete specfic classes by downcasting. Very counter-productive.

    How to avoid downcasting is a long story but here are a few basic steps you can take,

    1. Make each base class represent one single well defined concept. Then they will cover well for their derived classes.

    1. Make sure the base class is in a proper is-a relationship with all anticipated derived classes. Apple is-a Fruit works but ApplePie is-a Fruit doesn't.

    2. Prefer a base class to do things rather than being just a storage for setting and getting information. Make them agents of action rather than mere records for data shuffling.

    I find some of my pointers are not allowed to be converted by the compiler?
    That's what happens when you break the rules of the language so don't blame the compiler.
    Last edited by tiliavirga; July 1st, 2015 at 03:24 AM.

  10. #10
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Abstraction concept problem?

    pointers are evil
    and you should avoid them if possible

    that doesn't mean they're Always avoidable and aren't sometimes necessary.
    But as a whole, you tend to have cleaner and easier to use interfaces if you do avoid them.

  11. #11
    Join Date
    Jun 2015
    Posts
    208

    Re: Abstraction concept problem?

    Quote Originally Posted by OReubens View Post
    pointers are evil
    and you should avoid them if possible
    Congratulations!!! You've just won the Dogmatic Statement of the Month competition.

    I'll continue using every C++ feature for best effect and certainly pointers in OO paradigmic code where they are an indispensable tool (in the form of smart pointers of course) for high quality designs.

  12. #12
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Abstraction concept problem?

    Quote Originally Posted by tiliavirga View Post
    Congratulations!!! You've just won the Dogmatic Statement of the Month competition.
    it's not a dogma. it's guideline. That's a pretty different take on it.

    If you are working on projects that are long lived, and need to be maintained by many people, then the guideline is a good guideline to follow.

    Note that I also stated pointers in public interfaces are evil. It's hard to avoid all occurrences of them, so in private interfaces you will see them, as well as in the implementations of (member)functions.

  13. #13
    Join Date
    Jun 2015
    Posts
    208

    Re: Abstraction concept problem?

    Quote Originally Posted by OReubens View Post
    it's not a dogma. it's guideline.
    A guideline that is neither motivated nor qualified is a dogma.

    Besides, your guideline is out of touch with the consensus view. Avoiding or banning pointers was never an option. Neither Gosling of Java nor Hejlsberg of C# did away with pointers when they had the chance. Instead they domesticated them.

    Now Stroustrup of C++ also has tamed the pointers but with a twist. In true C++ spirit there is a choise. You can be nice and gentle for safety but also play it rougth where it counts. In advice form: Prefer pointer abstractions (like smart pointers and iterators) over raw pointers.
    Last edited by tiliavirga; July 3rd, 2015 at 01:24 AM.

  14. #14
    Join Date
    Oct 2008
    Posts
    1,456

    Re: Abstraction concept problem?

    Quote Originally Posted by tiliavirga View Post
    Value and reference semantics are complementary in modern C++. None is more evil than the other. None should be avoided more than the other.
    Congratulations!!! You've just won the Dogmatic Statement of the Month competition. ( July edition )

    >> None is more evil than the other

    this is hardly believable; references carry more responsabilities than the value they refer to ( you don't know who the reference refers to ), and pointers carry more responsabilities than references ( (smart) pointers have ownership; a pointer can point to nothing, even in a legal program; ... ).

    This adds a complexity cost and nothing can change this fact. Of course, this cost can be fair or even a great deal whenever those added responsabilities you paid for are used for something good.

    The evilness of pointers stands still though, because inexperienced programmers tend to pay this cost in exchange for nothing, as this very thread shows, resulting in bulimic base classes, downcasts, untestable spaghetti, ...

    for example, suppose we asked the OP to write classes modeling trains, made of locomotives and wagons; I could bet he'd think "hey, wagons can be linked to other wagons and a pair of locomotives, let's use (smart)pointers to model the links and add em' to the train instance ..." whoa, can you see where this will end ?

  15. #15
    Join Date
    Jun 2015
    Posts
    208

    Re: Abstraction concept problem?

    Quote Originally Posted by superbonzo View Post
    The evilness of pointers stands still though, because inexperienced programmers tend to pay this cost in exchange for nothing, as this very thread shows, resulting in bulimic base classes, downcasts, untestable spaghetti, ...
    You can hardly blame pointers for the lack of design & programming skills among C++ programmers.

    And even if you do the best medicine is not to dwelve on old C++ but to embrace modern C++ 11. It offers lots of safe and secure features suitable for less experienced programmers. Or use another language if you don't trust people to be able to learn and use modern C++.

    The "pointers are evil" dogma is an extreme position not shared by the best and brightest among language designers. They didn't abandon pointers when they had the chance. D even reinvented the raw C pointer. But most languages followed Java and domesticated the pointer and that's what C++ now finally has done too with version 11. Instead of raw pointers prefer pointer abstractions (such as smart pointers, iterators and implicit iteration constructs).

    Among languages C++ is something of an oddball because it has value types only and reference types must be simulated. But that doesn't mean reference types are evil and should be avoided. At least that's not the moderate mainstream position,

    https://msdn.microsoft.com/en-us/library/hh279654.aspx
    Last edited by tiliavirga; July 13th, 2015 at 01:18 AM.

Page 1 of 4 1234 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured