-4

If my pure virtual function is i.e.

virtual string func()=0;

is it returnable to an accessor function?

Edit: I apologize for earlier confusion, so I am adding a sample code

class Parent{
  public:
   parent(void){};
   virtual string func()=0;
   string getFunc (void) const{return var=func();} ;
  protected: 
   string var;
}
class Child: public  Parent{

public:
 class Child(void); 

string func(){
 string random // this is very generic I actually pretend 
               //to do some math here and return a string
 return random}; 

My intent is to then use the instantiated child object and ask the accessor(getFunc()) to return a value, that I can only compute based on the func(). But the error states that the nature of virtual function does not allow return, which frankly i find weird because it does have the return tag on it.

user207421
  • 305,947
  • 44
  • 307
  • 483
yurpmyguy
  • 5
  • 2
  • 2
    Sorry but what do you mean by "accessor function"? – Brian Bi Aug 07 '19 at 00:37
  • Do you mean something like `string getFrobaz() { return func(); }` – user4581301 Aug 07 '19 at 00:49
  • What happened when you tried it? – user207421 Aug 07 '19 at 01:09
  • 1
    @user207421 no, but it means maybe the question should explain or clarify what it is. – bolov Aug 07 '19 at 01:42
  • @bolov Explain an accessor function? Really? The word is used everywhere. Accessors and mutators (although not in this case, of course). – user207421 Aug 07 '19 at 02:21
  • @user207421 well, the five votes to close as unclear show that **maybe** yes it should. I personally can sincerely say I don't remember ever hearing the work "accessor function" in the context of C++. "getter and setters" yes, but not "accessor". Also it is very not clear to me what the "is it returnable to an accessor function?" is supposed to mean. So yes, the question is rightfully closed as unclear until the OP clarifies what he or she means. – bolov Aug 07 '19 at 02:31
  • @bolov I once stumbled over "accessor" as well and (after asking) I was taught that _accessor_ and _mutator_ are well defined terms [SO: What is the difference between accessor and mutator methods?](https://stackoverflow.com/a/9627148/7478597). Though, I personally find "getter" and "setter" more common as well... ;-) – Scheff's Cat Aug 07 '19 at 07:29
  • If I understood (and answered) your question right, the title may be better worded "Can a pure virtual function be called in an accessor function?". Otherwise, it sounds a bit like you want to return the virtual member function pointer... – Scheff's Cat Aug 07 '19 at 07:35

1 Answers1

-1

Calling a pure virtual function in another member function shouldn't be a problem at all. (Unfortunately, OP didn't copy/paste the exact error message.)

The exposed sample of OP has unfortunately a lot of typos, syntax errors, and other weaknesses.

Fixing this, I found one essential issue: the const-ness of Parent::getFunc().

Parent::getFunc() calls a non-const member function and mutates the member variable this->var which is not accepted by the compiler.

Removing the const, it worked.

Fixed sample:

#include <iostream>
#include <string>

class Parent{
  public:
    Parent() = default;

    virtual std::string func() = 0; // PURE VIRTUAL

    std::string getFunc() // WRONG: const
    {
      return var = func();
    }

  protected: 
    std::string var;
};

class Child: public Parent {

  public:
    Child() = default; 

    virtual std::string func() override
    {
      std::string random; // this is very generic I actually pretend 
               //to do some math here and return a string
      return random;
    }
};

#define DEBUG(...) std::cout << #__VA_ARGS__ << ";\n"; __VA_ARGS__ 

int main()
{
  DEBUG(Child child);
  DEBUG(child.getFunc());
}

Output:

Child child;
child.getFunc();

Live Demo on coliru


Though, a const accessor (I would call it “getter”) seems reasonable. With a little bit more re-design, it can be achieved as well:

#include <iostream>
#include <string>

class Parent{
  public:
    Parent() = default;

    virtual std::string func() const = 0; // PURE VIRTUAL

    std::string getFunc() const
    {
      return var = func();
    }

  protected: 
    mutable std::string var;
};

class Child: public Parent {

  public:
    Child() = default; 

    virtual std::string func() const override
    {
      std::string random; // this is very generic I actually pretend 
               //to do some math here and return a string
      return random;
    }
};

#define DEBUG(...) std::cout << #__VA_ARGS__ << ";\n"; __VA_ARGS__ 

int main()
{
  DEBUG(Child child);
  DEBUG(child.getFunc());
}

Output: the same like above

Live Demo on coliru

Note:

  1. I made the virtual func() const to make it accessable for const instances / member functions.

  2. The member variable became mutable to make it writable in const member functions.

I must admit that I personally find the mutable a bit scaring. In my simple mind, something is const or it isn't. However, the mutable just seems to be invented for update of internal caching of actually const objects. So, it might be the right tool here. (It's a little bit hard to say without more context.)

More about mutable: mutable specifier

Scheff's Cat
  • 19,528
  • 6
  • 28
  • 56