Thread: forward decleration in header file , regular use in cpp file

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    241

    forward decleration in header file , regular use in cpp file

    lets say we have a valid class with full implementation, X.
    can this class be declared as forward in Y header file:

    Code:
    class X;
    
    class Y{
    X* m_X;
    }

    but still be used as regular in the cpp file?
    Code:
    #include "Y.h"
    #incldue "X.h"
    
    T Y::function(){
    m_X->doSomething();
    }
    visual studio prevents me from doing it , I wonder if the standard also says so.

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Yes that should work.
    Quote Originally Posted by Dave11 View Post
    visual studio prevents me from doing it
    What sort of error do you get ?

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, you can do that if you are using a pointer to X. You can also have a reference to X.
    But if you have non-pointer or non-reference to X, you will get problems because the compiler cannot see the size of X, nor its constructors, copy constructors, operators and destructors.
    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. Replies: 4
    Last Post: 09-12-2014, 04:45 PM
  2. Replies: 16
    Last Post: 04-05-2014, 09:23 PM
  3. Replies: 11
    Last Post: 09-25-2011, 12:22 AM
  4. Replies: 30
    Last Post: 06-19-2006, 12:35 AM
  5. Replies: 4
    Last Post: 12-14-2005, 02:21 PM