Why returning a member variable using "this" pointer is not allowed, but setting a member variable using "this" is allowed?

tom_mai78101

Here's the header file code:

#ifndef __MANAGER_H__PROJECT_A__
#define __MANAGER_H__PROJECT_A__

#include <string>

class Manager {
private:
    std::string type;
    bool isStarted;
protected:
    void setType(std::string type);
public:
    Manager();
    virtual ~Manager();

    //Get type identifier
    std::string getType();

    //Startup manager. 0: Startup ok. Any #: Not ok.
    virtual int startUp();

    //Shutdown manager. 
    virtual void shutDown();

    //True: startUp executed ok. False, otherwise.
    bool isStarted() const;
};

#endif

The code below shows the error as "A pointer to a bound function may only be used to call the function".

bool Manager::isStarted() const {
    return this->isStarted;        //<----   ERROR.
}

And should be changed to this.

bool Manager::isStarted() const {
    return Manager::isStarted;     //<----   Correct.
}

So why is this acceptable?

void Manager::setType(std::string type){
    this->type = type;             //<----   Correct.
}

Thanks in advance.

aruisdante

Specifically, you've aliased isStarted to be the name of a method in the class, and thus this->isStarted is attempting to return a pointer to the member-method Manager::isStarted rather than some boolean value isStarted.

So your code that doesn't work doesn't work because it's not doing what you think it's doing. And your code that does work works because it's also not doing what you think it's doing; the returned pointer can be implicitly cast as a bool (and will always be equal to true), so you don't get a wrong return type error.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

setting a pointer member variable correctly

From Dev

In-class member initializer using a constructor: is it allowed?

From Dev

Using member variable as predicate

From Dev

Using member variable as predicate

From Dev

returning bool from member variable pointer

From Java

Pointer to member variable as static member

From Dev

Using Mockito WhiteBox for setting a member variable inside a method scope

From Dev

Why is variable sized object initialization not allowed using const

From Dev

Setting a member variable VS returning a value from a method

From Dev

Initializing member variable with pointer to itself

From Dev

pointer to member variable in nested struct

From Dev

Why a structure is allowed to have "pointer to its own type" as member but not "(an array of the) structure type" itself?

From Dev

Why Global variable redefinition is not allowed?

From Dev

Why is variable declaration not allowed here?

From Dev

Allow const member function to edit some member variable using mutable

From Dev

Using the address of a public member variable to access a private member

From Dev

Member variable aliasing in a simple struct using union

From Dev

Are there advantages of using const member variable in C++

From Dev

Are there advantages of using const member variable in C++

From Dev

variable initialization using class member function

From Dev

Is returning a private class member slower than using a struct and accessing that variable directly?

From Dev

Typecasting Member Variable Pointer to Object Pointer

From Java

Why defining buffer length of a buffer leads to that class's function member to loose the value of a function pointer member variable?

From Dev

Reach to array member using pointer

From Dev

issue with using pointer to member function

From Dev

Why is this allowed when using constexpr?

From Dev

Is returning by reference of a member variable EVER acceptable?

From Dev

Getting class and member type from pointer to member variable

From Dev

Error when using public member function to access private member variable: Variable "was not declared in this scope"

Related Related

  1. 1

    setting a pointer member variable correctly

  2. 2

    In-class member initializer using a constructor: is it allowed?

  3. 3

    Using member variable as predicate

  4. 4

    Using member variable as predicate

  5. 5

    returning bool from member variable pointer

  6. 6

    Pointer to member variable as static member

  7. 7

    Using Mockito WhiteBox for setting a member variable inside a method scope

  8. 8

    Why is variable sized object initialization not allowed using const

  9. 9

    Setting a member variable VS returning a value from a method

  10. 10

    Initializing member variable with pointer to itself

  11. 11

    pointer to member variable in nested struct

  12. 12

    Why a structure is allowed to have "pointer to its own type" as member but not "(an array of the) structure type" itself?

  13. 13

    Why Global variable redefinition is not allowed?

  14. 14

    Why is variable declaration not allowed here?

  15. 15

    Allow const member function to edit some member variable using mutable

  16. 16

    Using the address of a public member variable to access a private member

  17. 17

    Member variable aliasing in a simple struct using union

  18. 18

    Are there advantages of using const member variable in C++

  19. 19

    Are there advantages of using const member variable in C++

  20. 20

    variable initialization using class member function

  21. 21

    Is returning a private class member slower than using a struct and accessing that variable directly?

  22. 22

    Typecasting Member Variable Pointer to Object Pointer

  23. 23

    Why defining buffer length of a buffer leads to that class's function member to loose the value of a function pointer member variable?

  24. 24

    Reach to array member using pointer

  25. 25

    issue with using pointer to member function

  26. 26

    Why is this allowed when using constexpr?

  27. 27

    Is returning by reference of a member variable EVER acceptable?

  28. 28

    Getting class and member type from pointer to member variable

  29. 29

    Error when using public member function to access private member variable: Variable "was not declared in this scope"

HotTag

Archive