Home >Backend Development >C++ >Why Does Visual Studio 2015 Require \'&\' to Create a Pointer to a Member Function?

Why Does Visual Studio 2015 Require \'&\' to Create a Pointer to a Member Function?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-11-03 03:50:02484browse

Why Does Visual Studio 2015 Require

Non-Standard Syntax in Visual Studio 2015: Using '&' for Pointer to Member

Problem

When trying to access a member function in C without using the function call syntax, Visual Studio 2015 encounters an error: "non-standard syntax; use '&' to create a pointer to member."

Explanation

In Visual Studio, non-member functions can be used in expressions without using the function call syntax. However, for member functions, this is not allowed. To obtain a pointer to a member function, it is necessary to use the '&' operator.

Resolution

The solution to the error is to prefix the problematic member function name with the '&' operator, creating a pointer to the member function. By doing this, the function can be used in expressions without the function call syntax.

Example

Consider the following class definition:

<code class="cpp">class TicTacToe {
public:
    void player1Move(string coordX);
};</code>

To access the player1Move member function without using the function call syntax, use the following pointer to member:

<code class="cpp">TicTacToe::&player1Move;</code>

This pointer to member can now be used in expressions without calling the function explicitly:

<code class="cpp">TicTacToe Board;
(Board.*player1Move)("some_coordinate");</code>

By using the '&' operator, Visual Studio can correctly interpret the intent and avoid the non-standard syntax error.

The above is the detailed content of Why Does Visual Studio 2015 Require \'&\' to Create a Pointer to a Member Function?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn