Thread: Question: Determining object scope from root object

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 01: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, 11:52 AM
  4. Determining what object is closest:
    By Queatrix in forum C++ Programming
    Replies: 9
    Last Post: 08-16-2006, 11:18 PM