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
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
  • 174,939
  • 50
  • 355
  • 478
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
  • 335
  • 1
  • 11
10
votes
4 answers

Why does decltype(declval().func()) work where decltype(&T::func) doesn't?

I was trying to detect the presence of a member function baz() in a template parameter: template struct ImplementsBaz : public std::false_type { }; template struct ImplementsBaz :…
jtbandes
  • 115,675
  • 35
  • 233
  • 266
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
  • 3,166
  • 4
  • 29
  • 56
10
votes
3 answers

Wrong overload giving compiler error

Using VS2013, in the following example two different errors are given when attempting to pass a function to a worker's constructor, yet, lambda functions with the same prototype are ok. What am I doing wrong, and how can I change the definition of…
Steve
  • 613
  • 5
  • 15
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,995
  • 4
  • 20
  • 42
9
votes
3 answers

C++ member-function chaining return types and derived classes

Given this contrived example: struct point_2d { point_2d& x( int n ) { x_ = n; return *this; } point_2d& y( int n ) { y_ = n; return *this; } int x_, y_; }; struct point_3d : point_2d { point_3d& z( int n ) { z_ =…
Paul J. Lucas
  • 6,895
  • 6
  • 44
  • 88
9
votes
2 answers

Pass Member Function as Parameter to other Member Function (C++ 11 )

Let's say that I have a class with three member functions, as follows: #include #include class ClassName { public: double add(double a, double b); double intermediate(double a, double b, std::function
JohnTravolski
  • 131
  • 1
  • 1
  • 6
9
votes
1 answer

std::thread constructor Is there a difference between passing a pointer and passing by ref?

When creating a thread that calls a member function, is there a difference between passing a pointer to the current class or passing a reference? From the example below, does method1 behave the same as method2? Are there any differences? class…
9
votes
3 answers

static member function with C language binding?

The following C++ code compiles with Visual C++ and g++: struct S { static void foo(); }; extern "C" void S::foo() {} struct T { static void foo(); }; extern "C" void T::foo() {} auto main() -> int { S().foo(); T().foo(); } Is…
Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
9
votes
2 answers

Why does inlining my accessors break my code?

I am experiencing a strange issue where attempting to inline the accessors for my "Person" class causes the code to fail to compile. The following code will compile and run successfully (Using Visual Studio 2012): Person.h #pragma once #include…
tjwrona1992
  • 8,614
  • 8
  • 35
  • 98
9
votes
1 answer

Defining out-of-line member template functions

Consider this (minimized) example: template class hash_table { public: typedef int value_type; template void traverse (Argument); template
Mikhail Maltsev
  • 1,632
  • 11
  • 21
9
votes
7 answers

Is there a practical benefit to casting a NULL pointer to an object and calling one of its member functions?

Ok, so I know that technically this is undefined behavior, but nonetheless, I've seen this more than once in production code. And please correct me if I'm wrong, but I've also heard that some people use this "feature" as a somewhat legitimate…
Zoli
  • 1,137
  • 7
  • 12
9
votes
3 answers

Is it safe to place definition of specialization of template member function (withOUT default body) in source file?

Here's what I mean: // test.h class cls { public: template< typename T > void f( T t ); }; - // test.cpp template<> void cls::f( const char* ) { } - // main.cpp int main() { cls c; double x = .0; c.f( x ); // gives EXPECTED…
9
votes
1 answer

ref-qualified member functions as template arguments?

This compiles fine in clang 3.3: template struct M; template struct M { }; template struct M { }; but fails in gcc…
iavr
  • 7,547
  • 1
  • 18
  • 53