Getters overload in C ++?

Good afternoon,

I am not sure how to describe the process that I ask correctly in one sentence, so please excuse the title. I was looking for a way to ensure that base class and interface users will assign data that will be considered by the object itself and other non-default objects. So I did the following:

struct ExampleInterface { virtual void SomeMethod() = 0; virtual std::string WhatLooksLikeAGetterButIsNot() = 0; }; 

Here is an example of the real world:

 //So states can be "poped in and out".// struct State { //To retrive what the active state is called.// /*Code In Question--->*/virtual std::string RegardStateAs() = 0;/*<---Code In Question*/ virtual void ExecuteState( VRGE::MDNode* metaData ) = 0; }; 

The idea would be to end up doing something like A (This option helps prevent the problem if someone comes from "Update"):

 struct Update : public State { //Yadda yadda...// /*Code In Question--->*/std::string RegardStateAs() { return std::string{ "Update" }; }/*<---Code In Question*/ }; 

B (This option does not allow what A does):

 struct Update : public State { //Yadda yadda...// //Not a good example, but the point gets across.// Update( std::string value ) { stateName = value; } /*Code In Question--->*/virtual std::string RegardStateAs() { return stateName; }/*<---Code In Question*/ private: std::string stateName; }; 

My question is: is this good or bad practice?

----- ----- EDIT:

I do not have access to a compiler that can compile it, however, I was told that "overriding" would be ideal in this situation, for example:

 //So states can be "poped in and out".// struct State { //To retrive what the active state is called.// /*Code In Question--->*/virtual std::string RegardStateAs() = 0;/*<---Code In Question*/ virtual void ExecuteState( VRGE::MDNode* metaData ) = 0; }; struct Update : public State { //Yadda yadda...// /*Code In Question--->*/std::string RegardStateAs() override { return std::string{ "Update" }; }/*<---Code In Question*/ }; 
+4
source share
1 answer

As others have pointed out, your question is really about โ€œoverridingโ€ (in a polymorphic sense), not about โ€œoverloading.โ€

Assuming you need a fixed label for each state subclass (static), your strategy works, but there are some limitations. For clarity, I will refer to your "RegardAsState" as "ToString". If you need to call ToString in the constructor of the State base class, you will have problems. This is because the ToString V-table entry for a derived class, such as Update, is not yet set when the base class constructor is executed. A similar problem exists for calling ToString in the State destructor.

An alternative is to simply implement the status label as const std :: string in the State base class and return it to const std :: string and through the public get accessor. This is a bit more efficient both in eliminating the indirectness of the v-table and in resolving the return of const ref; and it fixes the above ctor / dtor problems in state.

Even the above sentence is not optimal, since there is no need to do this using the instance method if the label is the same for each individual state subclass. If you want to get exotic, you can implement ToString as a static (apatrium) accessor. This can be done with a little trick of passing a constant string as a template parameter, for example:

 template<const std::string & label> struct State { static const std::string & ToString() { return label; } }; extern const std::string UpdateLabel = "Update"; State<UpdateLabel> Update; // Or, with a bit of help from the macro preprocessor #define DEFINE_STATE(stateClass) \ extern const std::string stateClass##Label = #stateClass; \ struct stateClass##State : State<stateClass##Label> \ // continue with definition of stateClass##State ... {} DEFINE_STATE(Sample) { // implementation of SampleState }; 
+3
source

All Articles