πŸ“–
Go C++
  • Introduction
  • Chapter 1: What You Must Know First
    • Virtual Address Space of Process: Memory Partition and Layout
    • Function Call: Stack Frame
    • Program Compiling and Linking
  • Chapter 2: C++ Basics Improvement
    • Default Parameters
    • Inline Function
    • Function Overloading
    • new and delete
    • const and Pointers
    • References in Detail
  • Chapter 3: Object-Oriented Principles
  • Class and Object
  • Constructor and Destructor
  • Shallow Copy and Deep Copy
  • Initializer List
  • Various Member Functions
  • Pointer to Class Members
  • Chapter 4: Template Programming
  • Function Templates
  • Class Templates
  • Memory Allocators
  • Chapter 5: Operator Overloading
    • Operator Overloading
    • Introduction to Iterators
    • Issues of Iterator Invalidation
    • More about new and delete
    • Overloading of new and delete: Object Pool
  • Chapter 6: Inheritance and Polymorphism
    • Look inside Inheritance
    • More about Inheritance
    • Virtual Functions, Static Binding and Dynamic Binding
    • More about Virtual Functions
    • Understanding Polymorphism
    • Abstract Classes
    • Frequently Asked Interview Questions: Polymorphism
  • Chapter 7: Multiple Inheritance
    • Virtual Inheritance and Virtual Base Classes
    • Diamond Problem
    • Four Kinds of Type Conversions
  • Chapter 8: Standard Template Library
    • Sequence Containers
    • Container Adaptors
    • Associative Containers
    • More about Iterators
    • Function Objects
    • Generic Algorithms, Binders and Lambda Expressions
  • Chapter 9: Object Optimization
    • Behind the Object
    • Optimizing Objects in Functions
    • Member Functions with Rvalue References
    • Move Semantics and Perfect Forwarding
  • Chapter 10: Smart Pointers
    • Smart Pointers
    • Smart Pointers without Reference Counting
    • Smart Pointers with Reference Counting
    • Custom Deleters
  • Chapter 11: Function Objects and Binders
    • More about Binders
    • Introduction to std::function
    • Template Specialization and Argument Deduction
    • More about std::function
    • std::bind(): A Simple Thread Pool
    • More about Lambda Expressions
  • Chapter 12: Multithreading
    • Important Features in C++11
    • Multithreaded Programming with std::thread
    • Mutual Exclusion
    • Producer-Consumer Problem
    • Atomic Operations
    • Thread Visibility and volatile
  • Chapter 13: Design Patterns
    • Singleton Pattern
    • Factory Pattern
    • Proxy Pattern
    • Decorator Pattern
    • Adapter Pattern
    • Observer Pattern
Powered by GitBook
On this page
  • Pointer to Member Variables
  • Pointer to Member Functions
  • More About Function Pointers

Was this helpful?

Pointer to Class Members

Pointer to Member Variables

We all know that a pointer can used to represent the memory address of a variable. But what if the variable is inside a class? Let' look at this case.

class Test {
public:
    int ma;
    static int mb;
};
​
int Test::mb;

Now if we want to access the memory of Test::ma, we will get an error: cannot convert β€˜int Test::*’ to β€˜int*’ in initialization.

int main() {
    int *p = &Test::ma; // ERROR
    *p = 20;
    return 0;
}

This is because ma is within the scope of class Test, so the type of our pointer should also be modified with the qualifier. But another error occurs: invalid use of unary β€˜*’ on pointer to member.

int main() {
    int Test::*p = &Test::ma;
    *p = 20;        // ERROR
    return 0;
}

Remember that class is an abstract of instances, so the member variable is only valid within an object. And the following code is valid when we bound the pointer with an object.

int main() {
    Test *t = new Test();
    int Test::*p = &Test::ma;
    t->*p = 20;
    cout << t->ma << endl;  // 20
    delete t;
    return 0;
}

Notice that static member variables belong to the class instead of objects. So it is correct to access them with pointers, just like normal variables.

int main() {
    int *p = &Test::mb;
    *p = 30;
    cout << t->Test::mb << endl;    // 30
    return 0;
}

Pointer to Member Functions

In C and C++, we can have pointers to functions. This feature enables programmers to avoid code redundancy. Similarly, we can use pointers to access public member functions.

class Test {
public:
    void func() {
        cout << "call Test::func" << endl;
    }
    static void static_func() {
        cout << "call Test::static_func" << endl;
    }
};

Like member variables, the pointer is within the scope of the class, and has to be bounded with an object.

int main() {
    Test *t = new Test();
    void (Test::*pfunc)() = &Test::func;
    (t->*pfunc)();  //  call Test::func
    delete t;
    return 0;
}

Like static variables, static member functions can be pointed use normal function pointers.

int main() {
    void (*pfunc)() = &Test::static_func;
    (*pfunc)(); // call Test::static_func
    return 0;
}

More About Function Pointers

There may be ambiguity in using function pointers. In the following case all three statements can be compiled.

void foo() {...}
void bar() {
    void (*p1)() = foo;     // 1
    void (*p2)() = &foo;    // 2
    void (*p3)() = *foo;    // 3
}

But when it comes to member functions, only the second one can be compiled correctly.

class Test {
public:
    void foo() {...}
};
void bar() {
    void (Test::*p1)() = Test::foo;     // 1
    void (Test::*p2)() = &Test::foo;    // 2
    void (Test::*p3)() = *Test::foo;    // 3
}

The underlying principle is complicated, including implicit type conversion and polymorphism of member functions. Notice that C++ is a language with long history, and there are many historical issues. We don't have to, and can not understand all of them. In the above case, we only need to remember that & with function name is always a correct way in using function pointers.

For more explanations, please refer to C++ standard n3376 - 4.3 - 1 and n3337 - 5.3.1 - 4.

PreviousVarious Member FunctionsNextFunction Templates

Last updated 5 years ago

Was this helpful?