Obtaining object

This is a discussion on Obtaining object within the C++ Programming forums, part of the General Programming Boards category; I'm not quite sure how to phrase this but I'll take a stab, if you can't make sense of it ...

  1. #1
    Semi n00b Mithoric's Avatar
    Join Date
    Jun 2003
    Posts
    293

    Obtaining object

    I'm not quite sure how to phrase this but I'll take a stab, if you can't make sense of it tell me an I'll try again...

    Ok, say I have a class called MyClass. And MyObject is an object of type MyClass. Is it possible to obtain the object name in a function?

    Like say I have MyClass,

    Code:
    class MyClass{
    
       .... 
    
    };
    and I have:

    MyClass myObj1;
    MyClass myObj2;

    is it possible to get the name of the obj? Like say in a class function:

    Code:
    void MyClass::myFunction(...)
    {
      getObjName;
    }
    where get ObjName will return the parent or whatever, so if I go myObj1.myFunction() it will return myObj1 or if I do it with myObj2 it'll return myObj2 ???

    Hope that makes a little bit of sense... :|

  2. #2
    Registered User
    Join Date
    Sep 2003
    Posts
    23
    If you declare a class member with keyword static, it will be common to all objects in that class. Maybe you can use it in some way, for example to count instances and to assign different number to every instance, so you can distinguish between them.

    However, maybe it will be good to analyze the problem again and to find out whether something like that is really needed. What about some changes in your object analysis?

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    presumably, each object will have some sort of data. Have a copy of the data object name be a piece of the data:


    class MyObject
    public:
    int num;
    string name;

    MyObject myObj1;
    myObj1.name = "myObj1";


    to uniquely identify an object you can use it's address via the embedded this pointer or via the address of operator. Neither of these is the name, per se, though.

  4. #4
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    As mentioned, there is no way to do this built-in; it would be confusing if there were, because you can have objects that cannot be referenced by any variable name, and you can have many variable names which reference the same object.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    There's no way to do it directly from the language. Here's one example of doing it using inheritance and macros:

    Code:
    #ifndef _INF_DCL
    #define _INF_DCL
    #include <string>
    struct info{std::string tp_nm, ob_nm;};
    #ifndef store_info
    #define store_info(_decl_t_nm, _decl_o_nm)\
    _decl_o_nm.tp_nm\
    =""#_decl_t_nm""\
    ,_decl_o_nm.ob_nm\
    =""#_decl_o_nm""
    #endif
    #ifndef declare_info
    #define declare_info(_decl_t_nm, _decl_o_nm)\
    _decl_t_nm _decl_o_nm;\
    store_info(_decl_t_nm, _decl_o_nm)
    #endif
    #endif


    Then simply include the header, and derive classes from it:



    Code:
    #include "type_info.h"
    
    struct some : info
    {
    
    };
    
    int main(int argc, char *argv[])
    {
      declare_info(some, thing);
      
      cout << thing.tp_nm << endl 
           << thing.ob_nm << endl;
      
      some one;  
      
      store_info(some, one);
      
      cout << one.tp_nm << endl 
           << one.ob_nm << endl;
      
     return 0;
    }
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  6. #6
    Semi n00b Mithoric's Avatar
    Join Date
    Jun 2003
    Posts
    293
    I am currently just using a name variable, I was just hoping for an easier way.

    No problems though, I am just indetifying the variable which the problem exists in so I can track it down in my program.

    Like I have my log manager class, if I try to append to a log without one being open I get an error "No log open for %object%", that way I can realise I have not used object.openLog() before trying to append..

    Thanks anyway.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using this as synchronization object
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 03-22-2008, 07:49 AM
  2. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 08:38 PM
  3. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  4. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 04:24 PM
  5. Obtaining a random object with loops
    By Gnoober in forum C++ Programming
    Replies: 2
    Last Post: 10-12-2002, 02:31 PM

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