Forward declaration with boost::shared_ptr

This is a discussion on Forward declaration with boost::shared_ptr within the C++ Programming forums, part of the General Programming Boards category; Hey, I have a quick question. How can I forward declare something when I use boost::shared_ptr? Code: // File A ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485

    Forward declaration with boost::shared_ptr

    Hey, I have a quick question.

    How can I forward declare something when I use boost::shared_ptr?

    Code:
    // File A
    
    class ClassA;
    typedef boost::shared_ptr<ClassA> ClassA_ptr;
    
    class A
    {
    	Do cool stuff
    };
    
    
    // File B
    
    class ClassA;
    
    class B
    {
    public:
    	Do cool stuff
    
    private:
    	ClassA_ptr variableName // <-- How to forward declare this one?
    };
    Thanks

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    In theory, yes - but you have to explicitly define the destructor and make sure that ClassA is fully defined at that point.

    In practice, I've seen this fail on some compilers.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    Ok, so its safer and easier to just include the header then I guess.

    This is the first time im using smart pointers, so just one more quick question. Is there any problems when it comes to inheritance?

    EDIT: Never mind the last question. I was lazy and asked instead of checking it out myself. It looks like inheritance is no problem at all.
    Thanks for answering the original question CornedBee
    Last edited by h3ro; 03-15-2009 at 01:26 PM.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,027
    Although note that a shared_ptr<Derived> won't implicitly convert to shared_ptr<Base>. But you can use dynamic_pointer_cast to get around that.
    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.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Yes, it will. Or at least should. You should only need dynamic_pointer_cast to get from shared_ptr<Base> to shared_ptr<Derived>.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,027
    Funny. I could've sworn it didn't work before, but what do you know... it actually does work now.
    Problem solved, then.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  2. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 11:17 AM
  3. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM

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