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
9
votes
2 answers

Multiplying an object with a constant from left side

I have a Matrix class and it has overloaded * operators for scalar and matrix multiplications. template class Matrix { public: // ... Matrix operator*(T scalar) const; // ... } // ... template
8
votes
3 answers

How declare a C++ mem_fn(member_function) in function declaration?

I understand the basic problem in passing the address of a member function outside of its class. I get the feeling that mem_fn() might be the solution but I am having trouble with the specifics. I have a member function in class p that is currently…
Charles
  • 479
  • 1
  • 3
  • 13
8
votes
2 answers

Member access and template specialization

I have this class template template class Wrapper { public: virtual void parse(std::string s) = 0; protected: T value; }; ideally, each type should know how to parse itself from a string, so I would like to…
tunnuz
  • 23,338
  • 31
  • 90
  • 128
8
votes
3 answers

Whether to go for a member function or friend function when the function is supposed to change state of object?

In the book The C++ Programming Language, by Bjarne Stroustrup, the author introduces a class Matrix which has to implement a function inv(). In section 11.5.1, he talks about two possibilities of doing that. One is to make a member function and…
sajas
  • 1,599
  • 1
  • 17
  • 39
7
votes
2 answers

Is qualified name in the member function declaration allowed?

This code is accepted by MSVC9.0. My question is whether it is legal according to the standard (the old and/or the new one). A quote would be very much welcome, too. class X { void X::f(); };
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
7
votes
2 answers

How to prefer calling const member function and fallback to non-const version?

Consider a class Bar in two versions of a library: /// v1 class Bar { void get_drink() { std::cout << "non-const get_drink() called" << std::endl; } }; /// v2 class Bar { void get_drink() { std::cout << "non-const get_drink()…
Hank
  • 310
  • 1
  • 5
7
votes
3 answers

typedef return type in header and source files - what is best practice?

I have a class with a member function with a lengthy return type: /* MyClass.hpp */ namespace foo { class MyClass { public: SomeNameSpace::Lengthy_return_type_name someFunction(params...); }; } I want to make this more readable…
codemonkey789
  • 83
  • 1
  • 3
7
votes
1 answer

C++11 Lambda Functions inside member methods inherit scope

I've written a function foreach that accepts a lambda function ala: void foreach(void (*p)(pNode)) { /* ... */ } Which works as intended if I pass a lambda function from the main loop: int a = 5; env.N().foreach ( [&](pNode n)->void { …
jedwards
  • 29,432
  • 3
  • 65
  • 92
7
votes
2 answers

Is there any way to create a function that takes as argument a member function or a member?

I have a function like this: void f(std::ofstream& ostrm) { auto a = Myglobal->getData1(); ostrm << a; auto b = Myglobal->getData2(); ostrm << b; auto c = Myglobal->m_data1; ostrm << c; auto d = Myglobal->m_data2; …
Fractale
  • 1,503
  • 3
  • 19
  • 34
7
votes
2 answers

C++0x | Why std::atomic overloads each method with the volatile-qualifier?

The following excerpt from the current draft shows what I mean: namespace std { typedef struct atomic_bool { bool is_lock_free() const volatile; bool is_lock_free() const; void store(bool, memory_order =…
0xbadf00d
  • 17,405
  • 15
  • 67
  • 107
7
votes
4 answers

Returning pointer-to-member-function (without typedefs)

Compiling on C++03, I've attempted to write up a test template function that returns a pointer-to-member-function of a member function that returns int, and takes two float arguments: template int…
c1646091
  • 306
  • 1
  • 10
7
votes
2 answers

Are there other languages that have something like Swift's extensions?

In Swift, an extension is a way to define members for classes after the fact. Or, you could say, it is (coming from a total newb) fancy way of composing a function: extension Double { var mm: Double { return self * 1_000.0 } func mm1() ->…
hgiesel
  • 5,430
  • 2
  • 29
  • 56
7
votes
2 answers

How to specialize template member function?

I have the following template method: struct MyStruct { // ... template void readField(std::istream& in, T& data) { read(in, data); data = ntohl(data); } }; template<> void…
Patryk
  • 22,602
  • 44
  • 128
  • 244
7
votes
3 answers

get the real address(or index in vTable) of virtual member function

In c++ is there any way to get the real address of member function, or the index in vTable ? Updated: I don't know the INDEX in vTable and I don't know the address Here's why I want to know this: I want to hook the function ID3DXFont->DrawText of…
aj3423
  • 2,003
  • 3
  • 32
  • 70
7
votes
1 answer

What are the rules for function pointers and member function pointers to Standard functions?

What are the existing rules for taking function pointers or member function pointers to Standard functions? For example, something like auto p = &std::string::size; Is this legal? Would it be more or less legal if I explicitly requested the correct…
Puppy
  • 144,682
  • 38
  • 256
  • 465