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.

553 questions
129
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,…
106
votes
1 answer

error: default argument given for parameter 1

I'm getting this error message with the code below: class Money { public: Money(float amount, int moneyType); string asString(bool shortVersion=true); private: float amount; int moneyType; }; First I thought that default parameters…
pocoa
  • 3,749
  • 9
  • 35
  • 44
62
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,089
  • 3
  • 15
  • 31
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
44
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
  • 443
  • 1
  • 4
  • 4
44
votes
4 answers

C++ volatile member functions

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
  • 14,584
  • 15
  • 60
  • 93
42
votes
6 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…
Sam
  • 1,057
  • 5
  • 16
  • 20
39
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…
34
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
  • 120,726
  • 52
  • 304
  • 418
34
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,330
  • 1
  • 13
  • 18
33
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
  • 189,354
  • 112
  • 476
  • 821
30
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
  • 14,584
  • 15
  • 60
  • 93
27
votes
5 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
  • 27,743
  • 34
  • 143
  • 257
26
votes
3 answers

Free function versus member function

What is the advantage of having a free function (in anonymous namespace and accessible only in a single source file) and sending all variables as parameters as opposed to having a private class member function free of any parameters and accessing…
Jana
  • 4,806
  • 5
  • 20
  • 28
22
votes
4 answers

Does every c++ member function take `this` as an input implicitly?

When we create a member function for a class in c++, it has an implicit extra argument that is a pointer to the calling object -- referred as this. Is this true for any function, even if it does not use this pointer. For example, given the…
rtpax
  • 1,497
  • 12
  • 29
1
2 3
36 37