member-functions

Does a class with all attributes const need to have member function declared const as well?

限于喜欢 提交于 2020-02-04 00:13:38
问题 The title already says all. Let me expand a little nevertheless: I've class whose all attributes are const : template< class perm = Perm16 > class PermutationGroup { public: using StrongGeneratingSet = std::vector< std::vector< perm > >; const std::string name; const uint64_t N; const StrongGeneratingSet sgs; PermutationGroup(std::string name, uint64_t N, StrongGeneratingSet sgs) : name(name), N(N), sgs(sgs) { assert(check_sgs()); }; bool check_sgs() const; // defined as const bool is

Member function call in decltype

江枫思渺然 提交于 2020-01-31 03:09:32
问题 The following code: struct A { int f(int); auto g(int x) -> decltype(f(x)); }; Fails to compile with the error: error: cannot call member function 'int B::f(int)' without object If I change it to: struct A { int f(int); auto g(int x) -> decltype(this->f(x)); }; I get another error: error: invalid use of 'this' at top level What is wrong with either of these? I am using gcc 4.6 回答1: Currently you can only access 'this' and members of the class inside the function body, but this is likely to be

c++ assign a class member function a lambda function for computation efficiency [duplicate]

 ̄綄美尐妖づ 提交于 2020-01-30 06:29:44
问题 This question already has answers here : C++ lambda with captures as a function pointer (8 answers) Closed 4 years ago . UPDATED: (Rephrased) . I'm looking to boost the computation efficiency of my code by make an run-time assignment of a class member function to one of many functions conditional on other class members. One recommended solution uses #include <functional> and function<void()> , as shown in the simple test example: #include <iostream> using namespace std; struct Number { int n;

Is there any problem if I hold a member function pointer out of the pointer instance scope

孤街醉人 提交于 2020-01-17 22:26:47
问题 type A struct { x1 []int x2 []string } func (this *A) Test() { fmt.Printf("this is: %p, %+v\n", this, *this) } func main() { var fn func() { a := &A{} a.x1 = []int{1, 2, 3} a.x2 = []string{"one", "two", "three"} fn = a.Test } fn() } Please see: https://play.golang.org/p/YiwHG0b1hW- My question is: Will 'a' be released out of {} local scope? Is the lifetime of 'a' equal to the one of 'fn'? 回答1: Go is a garbage collected language. As long as you don't touch package unsafe (or similar like Value

Const reference qualifier on a member function [duplicate]

吃可爱长大的小学妹 提交于 2020-01-14 07:06:39
问题 This question already has answers here : What does the single ampersand after the parameter list of a member function declaration mean? (3 answers) Closed 5 years ago . I have seen in an anwser there: Is returning by rvalue reference more efficient? The member function definition: Beta_ab const& getAB() const& { return ab; } I am familiar with the cv-qualifier ( const ) on member functions, but not const& . What does the last const& mean? 回答1: The & is a ref-qualifier . Ref-qualifiers are new

Const reference qualifier on a member function [duplicate]

假装没事ソ 提交于 2020-01-14 07:04:54
问题 This question already has answers here : What does the single ampersand after the parameter list of a member function declaration mean? (3 answers) Closed 5 years ago . I have seen in an anwser there: Is returning by rvalue reference more efficient? The member function definition: Beta_ab const& getAB() const& { return ab; } I am familiar with the cv-qualifier ( const ) on member functions, but not const& . What does the last const& mean? 回答1: The & is a ref-qualifier . Ref-qualifiers are new

Passing member function to another object's member function C++

人走茶凉 提交于 2020-01-04 23:45:55
问题 I am having issues trying to pass a function as an argument in another object's function. I am well aware there are many similar topics but I either can't get their solution to work or can't understand them. class foo { public: void func1(void (*Drawing)(void)); template<class T> void func2(void (T::*Drawing)(void)); }; class bar { private: foo myFoo; void Drawing(); void func3() { // Attempt 1 myFoo.func1(Drawing); // Attempt 2 myFoo.func2<bar>(&bar::Drawing); } }; So in my first attempt, I

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

北慕城南 提交于 2020-01-03 15:49:48
问题 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 = Printer() printer.printMessage() How do I call the printMessage(self) method from another file, example.py in the same directory? I thought this answer was close, but it shows how to call a class method from another class within the same file. 回答1: You have to import it and call

const type qualifier soon after the function name [duplicate]

江枫思渺然 提交于 2020-01-01 08:34:07
问题 This question already has answers here : Meaning of 'const' last in a function declaration of a class? (9 answers) Closed 4 years ago . 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? 回答1: $9.3.1/3 states- "A nonstatic member function may be declared const, volatile, or const volatile. These cvqualifiers affect the type of the this

Why member functions can't be used as template arguments?

隐身守侯 提交于 2019-12-30 08:29:34
问题 Why member functions cannot be used as template arguments? For example, I want to do like: struct Foo { void Bar() { // do something } }; template <typename TOwner, void(&func)()> void Call(TOwner *p) { p->func(); } int main() { Foo a; Call<Foo, Foo::Bar>(&a); return 0; } I know that a similar thing can be done using pointers-to-member; well, it's cool enough most of the time, but I'm just curious about why pointers "should" be used. I see no ambiguity of interpreting "p->func()" above. Why