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
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
  • 159,303
  • 82
  • 478
  • 911
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
  • 6,783
  • 9
  • 50
  • 74
13
votes
4 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
  • 85,400
  • 43
  • 231
  • 337
13
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
  • 31,476
  • 15
  • 72
  • 121
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
  • 322
  • 4
  • 13
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
  • 435
  • 2
  • 9
11
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
  • 4,816
  • 5
  • 36
  • 45
11
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
  • 59,537
  • 86
  • 209
  • 308
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
  • 13,614
  • 8
  • 46
  • 134
11
votes
6 answers

Order of operator overload resolution involving temporaries

Consider the following minimal example: #include using namespace std; class myostream : public ostream { public: myostream(ostream const &other) : ostream(other.rdbuf()) { } }; int main() { cout <<…
Thomas
  • 150,847
  • 41
  • 308
  • 421
11
votes
2 answers

Finding constancy of member function

How can I detect a member function has const modifier or not? Consider the code struct A { int member(); int member() const; }; typedef int (A::*PtrToMember)(); typedef int (A::*PtrToConstMember)() const; I need something like this:…
Akon
  • 215
  • 1
  • 9
10
votes
4 answers

How to list the functions/methods of a javascript object? (Is it even possible?)

This question is intentionally phrased like this question. I don't even know if this is possible, I remember vaguely hearing something about some properties not enumerable in JS. Anyway, to cut a long story short: I'm developing something on a js…
BenoitParis
  • 2,363
  • 4
  • 25
  • 46
10
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,369
  • 1
  • 9
  • 18
10
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
  • 11,212
  • 2
  • 25
  • 52
9
votes
4 answers

Should I use static variables in my functions to prevent recomputing values?

I've been writing C++ code for a while now, but there's something I've been wondering for some time, without being to find a clear answer. My point here is the following: let's say I have a function (could be a method, could be static, but not…
Thomas Kowalski
  • 1,634
  • 2
  • 17
  • 30
1 2
3
36 37