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
33
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
  • 5,516
  • 5
  • 23
  • 29
24
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,687
  • 1
  • 18
  • 32
24
votes
1 answer

Is there a design reason why std::set doesnt have front and back member functions?

I know I can use *s.begin(), but same argument can be used for vector, which has front/back often I use the ordered property of set/map to get "smallest" element/key - ofc the fact that I do it is not the reason to have it, just a example :) Here…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
23
votes
2 answers

C++ GDB breakpoint for member functions

I am having trouble with using GDB on my c++ program. I want to set up a break point for my class member function and I'm not sure on the syntax of how to do it. My program is working find right now and I'm just trying to learn to use GDB. My…
user3543461
  • 243
  • 1
  • 2
  • 4
23
votes
2 answers

Const reference qualifier on a member function

I have seen in an anwser there: Is returning by rvalue reference more efficient? The member function definition: Beta_ab const& getAB() const& { return ab; } I am familiar with the cv-qualifier (const) on member functions, but not const&. What does…
galinette
  • 8,896
  • 2
  • 36
  • 87
22
votes
4 answers

static member functions and thread-safety

Objects and variables created in a static member function are not considered 'local' as they would in a member function, so that they can now be shared amongst multiple threads right? Whereas if you have a member function which creates some object,…
Tony The Lion
  • 61,704
  • 67
  • 242
  • 415
22
votes
5 answers

Why is calling a static member function with . or -> syntax legal?

Possible Duplicate: C++ Static member method call on class instance Today I discovered that something I had long (and I mean long—like, for twenty years), thought illegal in C++ is actually legal. Namely, calling a static member function as if it…
OldPeculier
  • 11,049
  • 13
  • 50
  • 76
21
votes
5 answers

Reasons for defining non-const 'get' member functions?

I'm working on learning C++ with Stroustrup's (Programming Principles & Practice Using C++) book. In an exercise we define a simple struct: template struct S { explicit S(T v):val{v} { }; T& get(); const T& get() const; void…
Juri
  • 604
  • 9
  • 19
21
votes
1 answer

In C++11, how do I specify that the implicit "this" parameter "[[carries_dependency]]"?

In [dcl.attr.depend]/1, I read: The attribute[...] carries_dependency [...] may be applied to the declarator-id of a parameter-declaration in a function declaration or lambda, in which case it specifies that the initialization of the parameter…
Marc Mutz - mmutz
  • 24,485
  • 12
  • 80
  • 90
20
votes
6 answers

Is there a way to call multiple functions on the same object with one line?

Just trying to tidy up a program and was wondering if anyone could feed me some syntax sugar with regard to calling a member function on one queue multiple times on the same line. For example, changing: queue q; q.push(0); q.push(1); to…
user6836841
20
votes
2 answers

Const member function and typedef, C++

Suppose we want to declare const member function via typedef: typedef int FC() const; typedef int F(); struct A { FC fc; // fine, we have 'int fc() const' const F f; // not fine, 'const' is ignored, so we have 'int f()' }; Since…
igntec
  • 1,006
  • 10
  • 24
20
votes
1 answer

Why can a static member function only be declared static inside the class definition and not also in its own definition?

While implementing a class for creating/updating boxes on the screen, I wanted to add a static member function that makes sure no currently visible boxes overlap (taking its information from a static pointer array to all currently visible boxes) My…
mr_T
  • 2,571
  • 3
  • 22
  • 39
19
votes
6 answers

Member function call in decltype

The following code: struct A { int f(int); auto g(int x) -> decltype(f(x)); }; Fails to compile with the error: error: cannot call member function 'int B::f(int)' without object If I change it to: struct A { int f(int); auto g(int…
HighCommander4
  • 50,428
  • 24
  • 122
  • 194
19
votes
3 answers

Is it possible to use member function call as default argument?

Here is my code: struct S { int f() { return 1; } int g(int arg = f()) { return arg; } }; int main() { S s; return s.g(); } This fails to compile with the error: error: cannot call member function 'int S::f()' without object Trying…
M.M
  • 138,810
  • 21
  • 208
  • 365
18
votes
2 answers

Why member functions can't be used as template arguments?

Why member functions cannot be used as template arguments? For example, I want to do like: struct Foo { void Bar() { // do something } }; template void Call(TOwner *p) { p->func(); } int main() { Foo…
Junekey Jeon
  • 1,496
  • 1
  • 11
  • 18
1
2
3
41 42