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
6
votes
1 answer

Memory model of template function inside non-template class?

Suppose I have: template class A { //Do something with T }; I know that the compiler will generate a class A for each different T defined in the code. What if I have: class B { template void f() { /* Do…
Shmoopy
  • 5,334
  • 4
  • 36
  • 72
6
votes
1 answer

Template member functions with trailing return type, giving errors even if unused

I understand that template member functions are only generated if used. This is convenient if not all used types support such a function. However, this does not appear to work for functions with trailing return type specification. Below is a small…
iavr
  • 7,547
  • 1
  • 18
  • 53
6
votes
2 answers

Specialization of template member functions of a non-template class

I would like to define the explicit specialization of a template function in a cpp file. Is that possible? To be more concrete, I have the following code, which compiles without errors: //class.h class myclass { public: /* Constructor */ …
Iban
  • 139
  • 2
  • 8
6
votes
2 answers

using and overloading a template member function of a base class?

In the following, struct Y overloads X's member function f. Both overloads are template functions, but take different arguments (typename and int), to be explicitly specified: struct X { template static bool f() { return true;…
iavr
  • 7,547
  • 1
  • 18
  • 53
6
votes
3 answers

Can unused member functions be reported by the linker? (C++)(gcc)

std::string has over 30 member functions that can be called on a string object. What if I only use a few of them? I assume the unused member functions will not take up space in the executable code section. I'm curious to know whether or not it's…
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
5
votes
3 answers

Member template functions cannot be virtual - workaround?

I understand why member template functions cannot be virtual, but I'm not sure what the best workaround is. I have some code similar to this: struct Entity { template virtual It GetChildren(It it) { return it; } }; struct…
user541686
  • 205,094
  • 128
  • 528
  • 886
5
votes
1 answer

Is there a way to have dynamic default arguments?

I'm trying to make a class where the user can modify member variables to change the default arguments of its member functions. class Class { public int Member; public void Method(int Argument = Member) { // This compiles fine,…
Maxpm
  • 24,113
  • 33
  • 111
  • 170
5
votes
2 answers

Mutual return types of member functions (C++)

Is it possible in C++ to have two classes, let's call them A and B, such that A has a member function f that returns an object of class B, and B has a member function g that returns an object of class A? (The text below is just to show I have "done…
Veky
  • 53
  • 2
5
votes
0 answers

Can CLion move an in-the-class method definition out-of-class?

Common background goes first. In C++, you can write a method definition right inside the class body, as illustrated in the following Effective-C++ styled Widget class: class Widget { unsigned TheSize; public: unsigned getSize() const { return…
cgsdfc
  • 538
  • 4
  • 19
5
votes
2 answers

How to call a template member function in a template base class?

When calling a non-templated member function in a base class one can import its name with using into the derived class and then use it. Is this also possible for template member functions in a base class? Just with using it does not work (with…
Lars
  • 2,616
  • 3
  • 21
  • 18
5
votes
2 answers

Member function decorator and self argument

The following minimal example of a decorator on a member function: def wrap_function(func): def wrapper(*args, **kwargs): print(args) print(kwargs) return wrapper class Foo: @wrap_function def mem_fun(self, msg): …
Tobias Hermann
  • 9,936
  • 6
  • 61
  • 134
5
votes
4 answers

c++: what's the design philosophy of allowing temporary object to call non-const member function?

I search the stack overflow and people say it's stupid to modify temporary object, so binding temporary object to non-const lvalue reference is not allowed, like you can't pass a temporary object to a function with non-const lvalue reference. Then…
Han XIAO
  • 1,148
  • 9
  • 20
5
votes
2 answers

Calling volatile member function using not volatile object in C++

What happens, If calling volatile member function using not volatile object? #include using namespace std; class A { private: int x; public: void func(int a) volatile //volatile function { x = a; …
msc
  • 33,420
  • 29
  • 119
  • 214
5
votes
1 answer

How to declare a friend that is a member function of another not yet defined class in C++?

How I declare B's constructor to be a friend of A? I tried: class A { private: A(); public: friend B::B(); }; class B { public: B(); };
IamMan
  • 322
  • 1
  • 2
  • 13
5
votes
4 answers

How to tell if class contains a certain member function in compile time

Possible Duplicate: Is it possible to write a C++ template to check for a function's existence? say there are 2 classes: struct A{ int GetInt(){ return 10; } }; struct B{ int m; }; I want to use object of type A or B in following…
JQ.
  • 678
  • 7
  • 17