-4

I have function get_time() in a class which return a private member.

How can I use the object which called this function in the implementation of the function.

For example in case comp object which have a member call name call get_time function (comp.get_time()): I want to be able to get the comp.name in the implementation of the det_time_function. How can I?

e.g

class comp
{
   public:
   string name;
}

class calc : public comp
{ 
   private:
   int time;
   public:
   int get_time(){
     ///here I want to get the name of the object which call the calc
    ///should I use this.name?
   } 
}

calc calc_obj;
calc.get_time();
Sarit8
  • 33
  • 5
  • 2
    Code. Share your code. (Specifically the class) We can give you advice, but seeing your code makes sure that we give you advice that we'll be confident will work for your environment. That way someone doesn't take the time to give you a full-fledged answer and then you go, "Oh wait, I initialized my class like this with these parameters so your answer doesn't work for my case." :] – 8protons May 05 '16 at 15:34
  • use this (http://www.tutorialspoint.com/cplusplus/cpp_this_pointer.htm) – Ahmed Gamal May 05 '16 at 15:35
  • 1
    What is stopping you from "seeing" the private members from within the public member function definition?? and using `this->` pointer will help you too – Khalil Khalaf May 05 '16 at 15:40
  • 8protons- I added some code. – Sarit8 May 05 '16 at 15:56
  • [May be book time, Sarit.](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) It reads like you are missing some fundamentals and don't know the right terms to explain what you want, or you're missing some fundamentals and tripping over something trivial. Either way, you're probably better helped by upping your C++ knowledge. – user4581301 May 05 '16 at 16:05

2 Answers2

0

Derived classes can not access the private members of their base classes. However, if you use protected declaration instead of private, then you could do that. But another way (which you should practice and learn) is by providing a getter() function in your parent class which return its object's name, then you can use that getter to call it from the child class and get the private member of the parent class; name in your case.

A basic getter() looks something like this:

YourVariableTypeHere get_VariableName()
{
     return this->VariableName;
}
Khalil Khalaf
  • 9,259
  • 11
  • 62
  • 104
0
int get_time(){
  ///here I want to get the name of the object which call the calc
  ///should I use this.name?
} 

No. To fetch the base class public data attribute, you would use

comp::name

Note, however, that in general, public data attributes should be avoided (because this disables encapsulation).


Also, in this question, it appears to be possible that some other object or function can call the derived class method, not simply the base class. So perhaps your question is non-sequitor, or perhaps misleading.

One way to provide a 'label' to handle both cases is to include a string in the method.

calc::get_time("caller-name");

Now the name is provided to the method, and the method need not fetch it from the base class, nor from which ever invoking object called get_time().

2785528
  • 5,438
  • 2
  • 18
  • 20