Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. What is the difference between these 2 ways of declaring an object?

What is the difference between these 2 ways of declaring an object?

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    KaKa
    wrote on last edited by
    #1

    While I was reading some C++ code, I saw a code snippet which declared an object like this:

    #include <string>
    #include <cassert>
    #include "BaseGameEntity.h"
    #include "Locations.h"

    class State;

    class Miner : public BaseGameEntity
    {
    private:

      State\* m\_pCurrentState; 
      .....
    

    };

    Another way of declaring an object is to include the header file and then declare the object in the class:

    #include <string>
    #include <cassert>
    #include "BaseGameEntity.h"
    #include "Locations.h"
    #include "State.h"

    class Miner : public BaseGameEntity
    {
    private:

      State m\_pCurrentState; 
      .....
    

    };

    Yes, in the first code snippet, an object pointer is declared while in the second one, it is a normal object. Other than that,is there difference between these 2 ways of declaring an object? When should either method be used? :confused:

    J 1 Reply Last reply
    0
    • K KaKa

      While I was reading some C++ code, I saw a code snippet which declared an object like this:

      #include <string>
      #include <cassert>
      #include "BaseGameEntity.h"
      #include "Locations.h"

      class State;

      class Miner : public BaseGameEntity
      {
      private:

        State\* m\_pCurrentState; 
        .....
      

      };

      Another way of declaring an object is to include the header file and then declare the object in the class:

      #include <string>
      #include <cassert>
      #include "BaseGameEntity.h"
      #include "Locations.h"
      #include "State.h"

      class Miner : public BaseGameEntity
      {
      private:

        State m\_pCurrentState; 
        .....
      

      };

      Yes, in the first code snippet, an object pointer is declared while in the second one, it is a normal object. Other than that,is there difference between these 2 ways of declaring an object? When should either method be used? :confused:

      J Offline
      J Offline
      Jijo Raj
      wrote on last edited by
      #2

      KaKa' wrote:

      Yes, in the first code snippet, an object pointer is declared while in the second one, it is a normal object. Other than that,is there difference between these 2 ways of declaring an object?

      The first code snippet, you have forward declared the class. So you don't need to include the corresponding header file. If both classes are independent one, it doesn't matter. Both code snippet work well. Well, it does matter when you have two classes which circular dependency. In that case, your second code snippet won't work. Check out this link[^] for more information. So, in summery, both code snippets are not exactly the same in all conditions. :) Regards, Jijo.

      _____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.

      K 1 Reply Last reply
      0
      • J Jijo Raj

        KaKa' wrote:

        Yes, in the first code snippet, an object pointer is declared while in the second one, it is a normal object. Other than that,is there difference between these 2 ways of declaring an object?

        The first code snippet, you have forward declared the class. So you don't need to include the corresponding header file. If both classes are independent one, it doesn't matter. Both code snippet work well. Well, it does matter when you have two classes which circular dependency. In that case, your second code snippet won't work. Check out this link[^] for more information. So, in summery, both code snippets are not exactly the same in all conditions. :) Regards, Jijo.

        _____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.

        K Offline
        K Offline
        KaKa
        wrote on last edited by
        #3

        Thanks alot Jijo......that explains alot :) For the second code snippet with the class forward declaration, does this method work if the forward declared class file is located in another directory?

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups