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

How to use variadic templates to make a generic Lua function wrapper?

For my current project, I've been writing a lot of C/C++ to Lua wrappers. A large number of these are simple setters and getters, so I managed to write some templates that make it easy to generate these, like so: // Class Return …
Alex
  • 14,973
  • 13
  • 59
  • 94
6
votes
1 answer

C++ pointer-to-method template deduction doesn't compile when targeting x86, but works with x64

I've got this sample code: struct A { int foo() { return 27; } }; template struct Gobstopper { }; template<> struct Gobstopper { Gobstopper(int, int) { } // To differentiate from general Gobstopper…
6
votes
3 answers

why can't a volatile object call nonvolatile member function

Why can't a volatile object call a non-volatile member function? In case of const, it makes sense that calling a non-const member function violates the constness of the the object and hence it is prohibited. But why in the case of volatile?
Chethan
  • 905
  • 1
  • 10
  • 18
6
votes
2 answers

Is it safe to use std::bind with boost::signals2?

Is it safe to use std::bind to pass a member function to boost::signals2::signal::connect()? In other words, is boost::bind and std::bind interchangeable? It compiles with VC++ 2010 SP1, but the template code is way over my head and I'm afraid I…
links77
  • 1,534
  • 4
  • 14
  • 20
6
votes
1 answer

member function that is not const should only be constexpr if on C++14 or later

In C++11, constexpr on a member function implies const. That was changed in C++14. I have some code that has a member function that should be constexpr, but cannot be const, so I'd like it to be constexpr if compiled with std c++14 or later. One…
Philipp
  • 957
  • 1
  • 6
  • 20
6
votes
6 answers

static var in member function

bool SomeClass::Function( bool thankYou = true ) { static bool justAbool = false; // Do something with justAbool; ... } I have searched around but I can't find anything about this except globals vars or member functions itself. What…
Steijn
6
votes
1 answer

About pointer to member function of derived class

Here's my code, and the IDE is DEV C++11 #include using namespace std; class A{ public: int a=15; }; class B:public A { }; int main(){ int A::*ptr=&B::a; //OK int B::*ptr1=&A::a; //why? int…
BooAA
  • 173
  • 3
  • 11
6
votes
2 answers

In C++ (class), do I always need to declare function in the header file?

For example, I have a markerdisplay.cpp file. The markerdisplay member function will look like the below code. void MarkerDisplay::setMarkerStatus(MarkerID id, StatusLevel level, const std::string& text) { ..... } Can…
Allen Yuan
  • 77
  • 1
  • 6
6
votes
2 answers

F# Recursive Member Function : "How to Define it correctly"

I it is my understanding that when you define a recursive member function within a type then there is no need to define the function to be recursive. Meaning use of the rec keyword. however when i do this: type hello() = class member…
Nulle
  • 1,301
  • 13
  • 28
6
votes
0 answers

C++ When to use `const` vs `const &` as member function modifier?

I am learning C++. One of the things I have trouble to understand, is when to put &, const or const & after the declaration of a member function. An example: class Foo { // When to use which one? bool bar(int baz); bool bar(int baz)…
Qqwy
  • 5,214
  • 5
  • 42
  • 83
6
votes
1 answer

Forward member function to static method

Context Basically I need to have a const void *const for a member function, because that has to be passed to a third party library (which means I cannot use bind, function, etc). Since that seems to be impossible, I wanted to do the next best thing…
atlaste
  • 30,418
  • 3
  • 57
  • 87
6
votes
1 answer

Function taking both pointer to member-function and pointer to const member-function

I have the following code base: template class SomeClass { public: template void register_function(const std::pair fct) { auto f…
6
votes
3 answers

Passing a C++ Member Function Pointer to an STL Algorithm

I have a member function as follows: class XYZ{ public: float function(float x); private: float m_DensityMin; float m_DensityMax; }; Now, I'm trying to transform a std::vector foo using the std::transform STL algorithm by passing…
ccoder83
  • 504
  • 6
  • 15
6
votes
2 answers

Defining unnamed class member functions?

I currently have two unnamed classes defined in my Foo.h: class Foo { public: Foo(); class { private: int x; int y; public: int GetX() { return x; } int GetY() { return y; } } Sub1; class { …
jvpernis
  • 195
  • 8
6
votes
5 answers

Class member functions instantiated by traits [policies, actually]

I am reluctant to say I can't figure this out, but I can't figure this out. I've googled and searched Stack Overflow, and come up empty. The abstract, and possibly overly vague form of the question is, how can I use the traits-pattern to…
Jive Dadson
  • 16,680
  • 9
  • 52
  • 65