Like Tree1Likes
  • 1 Post By laserlight

Question: Determining object scope from root object

This is a discussion on Question: Determining object scope from root object within the C++ Programming forums, part of the General Programming Boards category; Merry Christmas and a Happy New Year. This board is an invaluable resource and I thank you all throughout the ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    178

    Question: Determining object scope from root object

    Merry Christmas and a Happy New Year. This board is an invaluable resource and I thank you all throughout the year for your support.

    Now, onto my question, I have defined a nested structure as follows:

    Code:
    typedef struct foo{
         
         typedef struct poo{
         }poo;
    
         typedef struct shoo{
         }shoo;
    
    }foo;
    I define a function as follows:

    Code:
    void placeObject(foo object);
    I would like to pass a parameter such as:

    Code:
    foo:poo object;
    placeObject(object);
    Then the function placeObject determines the type of foo object passed in but this is proving more difficult than I thought.

    It appears I would have to overload placeObject with each scoped inner struct such as placeObject(foo::shoo do) but not sure I have to do that. Any ideas on how placeObject can have a type foo argument and figure out the inner struct type used?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Perhaps you should consider the use of a class hierarchy instead: foo could be a polymorphic base class, then poo and shoo derive from it, overriding certain methods that are then invoked by placeObject. Under ideal circumstances, you then would not need to find out the type of the object as polymorphism would just work to call the desired method.
    Elysia likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    178
    Interesting idea laserlight. I assume this also would work better with classes rather than structures too?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Other than rules pertaining to default access (private for classes and public for structs), classes and structs are pretty much equivalent in C++.

    Also, you don't need those typedefs either way.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    178
    I thought the typedef made it easier to refer to the struct more similar to a variable:

    Code:
    foo variable_name;
    No?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Quote Originally Posted by Imanuel
    I thought the typedef made it easier to refer to the struct more similar to a variable
    In C, the typedef is used such that one can use the name alias foo instead of struct foo. In C++, foo would already be the type name.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    178
    Ahh ok, thank you. More of my old C habits ...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble storing ifstream object in pair object
    By Programmer_P in forum C++ Programming
    Replies: 7
    Last Post: 01-17-2012, 12:20 AM
  2. Object not declared in this scope
    By -EquinoX- in forum C++ Programming
    Replies: 1
    Last Post: 08-30-2009, 01:31 AM
  3. Replies: 4
    Last Post: 11-14-2006, 10:52 AM
  4. Determining what object is closest:
    By Queatrix in forum C++ Programming
    Replies: 9
    Last Post: 08-16-2006, 11:18 PM

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