Home > Backend Development > C++ > body text

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

Mary-Kate Olsen
Release: 2024-11-03 03:50:02
Original
381 people have browsed it

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>
Copy after login

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

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

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template