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
17
votes
4 answers

Template member function specialization in a template class

I have a template class and a member function print() to print the data. template class A { public: T data; void print(void) { std::cout << data << std::endl; } // other functions ... }; Then, I want to either…
17
votes
1 answer

Pointer to function members: what does `R(*C::*)(Args...)` mean?

Consider the following code: template struct test: std::integral_constant {}; template struct test: std::integral_constant {}; template
Vincent
  • 57,703
  • 61
  • 205
  • 388
17
votes
6 answers

Non-member vs member functions in Python

I'm relatively new to Python and struggling to reconcile features of the language with habits I've picked up from my background in C++ and Java. The latest issue I'm having has to do with encapsulation, specifically an idea best summed up by Item…
JimmidyJoo
  • 10,503
  • 7
  • 27
  • 30
16
votes
6 answers

Why doesn't the program crash when I call a member function through a null pointer in C++?

#include "iostream" using namespace std; class A { public: void mprint() { cout<<"\n TESTING NULL POINTER"; } }; int main() { A *a = NULL; a->mprint(); return 0; } I am getting output as "TESTING NULL POINTER". Can…
user258367
  • 3,247
  • 2
  • 18
  • 17
16
votes
3 answers

When do we need a .template construct

I made the following program #include #include template struct Class { template void display(){ std::cout<
Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
15
votes
5 answers

What's the best way to sum the result of a member function for all elements in a container?

Let's say I have the following object: struct Foo { int size() { return 2; } }; What's the best way (most maintainable, readable, etc.) to get the total size of all objects in a vector? I'll post my solution but I'm interested in better…
Michael Kristofik
  • 34,290
  • 15
  • 75
  • 125
14
votes
4 answers

How can C++ virtual functions be implemented except vtable?

Possible Duplicate: A question about virtual mechanism in C++ Is using vtable the only way to implement virtual member functions mechanism in C++? What other ways exist?
sharptooth
  • 167,383
  • 100
  • 513
  • 979
14
votes
3 answers

How do I call a class method from another file in Python?

I'm learning Python and have two files in the same directory. printer.py class Printer(object): def __init__(self): self.message = 'yo' def printMessage(self): print self.message if __name__ == "__main__": printer =…
Thomas
  • 5,810
  • 7
  • 40
  • 48
14
votes
12 answers

What is the practical use of pointers to member functions?

I've read through this article, and what I take from it is that when you want to call a pointer to a member function, you need an instance (either a pointer to one or a stack-reference) and call it…
slashmais
  • 7,069
  • 9
  • 54
  • 80
13
votes
4 answers

callback from c++ to objective c

I have ViewController in objective-c and most of my code is c++ (.mm). I'd like to setup some callbacks to member functions from obj-c (in c++) and call them from c++. Something like this (it's very simplifyed): @interface MyClass {…
velkyel
  • 352
  • 4
  • 13
13
votes
3 answers

Why class member functions shadow free functions with same name?

It recently came to my attention that member functions completely shadow free functions with the same name when inside the class. And by completely I mean that every free function with the same name is not considered for overload resolution at all.…
yuri kilochek
  • 12,709
  • 2
  • 32
  • 59
12
votes
1 answer

Why is "a.template foo<0>();" allowed even though "a.foo<0>();" is enough?

struct A { template void foo() {} }; int main() { A a; a.foo<0>(); // ok a.template foo<0>(); // also ok } Obviously, a.foo<0>(); is more concise, intuitive, and expressive than a.template foo<0>();. Why does C++ allow…
szxwpmj
  • 465
  • 2
  • 10
12
votes
3 answers

const type qualifier soon after the function name

In C++ sometimes I see declarations like below: return_type function_name( datatype parameter1, datatype parameter2 ) const { /*................*/} What does this const type qualifier exact do in this case?
Vijay
  • 65,327
  • 90
  • 227
  • 319
11
votes
1 answer

Forwarding cv-ref-qualifier for member functions

If there are no another overloadings (say, f(T &) or f(volatile T &&)) of a (member) function template template< typename T > f(T &&);, then T && is so-called forwarding reference, and T is either U, or U & for some cv-qualified type U. But for…
Tomilov Anatoliy
  • 15,657
  • 10
  • 64
  • 169
11
votes
1 answer

F# calling member functions in constructor

I am writing an F# type, and I'm having trouble figuring out how to reference a member function from the constructor upon initialization. I think I'm supposed to use a do binding, but then the do binding can't understand the member functions. Is…
user3685285
  • 6,066
  • 13
  • 54
  • 95
1 2
3
41 42