Questions tagged [member-functions]

A function declared and/or defined within a class.

A member function is a procedure that whose signature is declared inside a class. This function takes an implicit argument of the same type as the containing class and is only accessible via an object of that type. When the function is static, no implicit argument is needed, but the function must be invoked via the class' scope. In contrast, a free-function does not have an implicit argument of the class type nor is invoked via a class scope.

626 questions
866
votes
12 answers

What is the meaning of 'const' at the end of a member function declaration?

What is the meaning of const in declarations like these? class foobar { public: operator int () const; const char* foo() const; };
user91111
149
votes
2 answers

Operator overloading : member function vs. non-member function?

I read that an overloaded operator declared as member function is asymmetric because it can have only one parameter and the other parameter passed automatically is the this pointer. So no standard exists to compare them. On the other hand,…
129
votes
8 answers

Function pointer to member function

I'd like to set up a function pointer as a member of a class that is a pointer to another function in the same class. The reasons why I'm doing this are complicated. In this example, I would like the output to be "1" class A { public: int f(); int…
Mike
  • 58,961
  • 76
  • 175
  • 221
67
votes
5 answers

What are all the member-functions created by compiler for a class? Does that happen all the time?

What are all the member-functions created by compiler for a class? Does that happen all the time? like destructor. My concern is whether it is created for all the classes, and why is default constructor needed?
Onnesh
  • 1,169
  • 3
  • 15
  • 31
65
votes
3 answers

what does "error : a nonstatic member reference must be relative to a specific object" mean?

int CPMSifDlg::EncodeAndSend(char *firstName, char *lastName, char *roomNumber, char *userId, char *userFirstName, char *userLastName) { ... return 1; } extern "C" { __declspec(dllexport) int start(char *firstName, char *lastName, char…
Oscar Yuandinata
  • 653
  • 1
  • 5
  • 4
49
votes
5 answers

Can C++ struct have member functions?

I was pretty confused about the difference between struct and class as I seemed to see them used for pretty much the same things. I googled the differences and the only answer I saw was that structs have public members by default and classes have…
sion
  • 1,367
  • 6
  • 17
  • 21
46
votes
4 answers

Do non-static member variables in a C++ struct/class need to be marked as volatile to be treated as volatile in a member function?

class MyClass { int x, y; void foo() volatile { // do stuff with x // do stuff with y } }; Do I need to declare x and y as volatile or will be all member variables treated as volatile automatically? I want to make…
0xbadf00d
  • 17,405
  • 15
  • 67
  • 107
46
votes
4 answers

Why can some operators only be overloaded as member functions, other as friend functions and the rest of them as both?

Why can some operators only be overloaded as member functions, other as non-member "free" functions and the rest of them as both? What is the rationale behind those? How to remember which operators can be overloaded as what (member, free, or both)?
S I
45
votes
7 answers

Effective C++ Item 23 Prefer non-member non-friend functions to member functions

While puzzling with some facts on class design, specifically whether the functions should be members or not, I looked into Effective c++ and found Item 23, namely, Prefer non-member non-friend functions to member functions. Reading that at first…
40
votes
5 answers

Print address of virtual member function

I am trying to print the address of a virtual member function. If I know which class implements the function I can write: print("address: %p", &A::func); But I want to do something like this: A *b = new B(); printf("address: %p", &b->func);…
hidayat
  • 9,493
  • 13
  • 51
  • 66
39
votes
4 answers

Get memory address of member function?

How do I get the absolute address of a member function in C++? (I need this for thunking.) Member function pointers don't work because I can't convert them to absolute addresses (void *) -- I need to know the address of the actual function in…
user541686
  • 205,094
  • 128
  • 528
  • 886
38
votes
5 answers

C++ typedef member function signature syntax

I want to declare type definition for a member function signature. Global function typedefs look like this: typedef int (function_signature)(int, int); typedef int (*function_pointer) (int, int); But I'm not able to the same thing for a member…
0xbadf00d
  • 17,405
  • 15
  • 67
  • 107
36
votes
9 answers

Nonstatic member as a default argument of a nonstatic member function

struct X { X():mem(42){} void f(int param = mem) //ERROR { //do something } private: int mem; }; Can anyone give me just one reason as to why this is illegal in C++?! That is to say, I know that it is an error, I know what the…
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
35
votes
6 answers

How to directly bind a member function to an std::function in Visual Studio 11?

I can easily bind member functions to a std::function by wrapping them with a lambda expression with capture clause. class Class { Class() { Register([=](int n){ Function(n); }); } void Register(std::function
danijar
  • 32,406
  • 45
  • 166
  • 297
35
votes
1 answer

std::mem_fun vs std::mem_fn

What is the difference between std::mem_fun and std::mem_fn? Why is the naming so confusing? Boost's documentation says that std::mem_fn can replace std::mem_fun in most cases. So in what situation would you still use std::mem_fun?
Scotty
  • 2,480
  • 2
  • 16
  • 20
1
2 3
41 42