member-functions

Non-member vs member functions in Python

浪子不回头ぞ 提交于 2021-02-07 04:52:28
问题 I'm relatively new to Python and struggling to reconcile features of the language with habits I've picked up from my background in C++ and Java. The latest issue I'm having has to do with encapsulation, specifically an idea best summed up by Item 23 of Meyer's " Effective C++ ": Prefer non-member non-friend functions to member functions. Ignoring the lack of a friend mechanism for a moment, are non-member functions considered preferable to member functions in Python , too? An obligatory,

Why can't you partially specialize a class member function?

て烟熏妆下的殇ゞ 提交于 2021-01-29 05:03:36
问题 Member functions of template classes can be fully specialized, e.g. template<class A> struct MyClass { // Lots of other members int foo(); }; template<class A> MyClass<A>::foo() { return 42; } template<> MyClass<int>::foo() { return 0; } would compile without problems. Note that foo() is not a template function so this is not about template function specialization (I can understand partial specialization is not allowed there as it would become incredibly confusing in combination with

template member function is instantiated only if called

你说的曾经没有我的故事 提交于 2021-01-19 04:44:18
问题 Why there is an error in this code: template <typename T> class CLs{ public: void print(T* p){ p->print(); } }; void main() { CLs<int> c1; // compilation OK CLs<double> c2; // compilation OK double d=3; c2.print(&d); } My lecturer said there is an error in the c2.print(&d); line: Compilation Error: Member function is instantiated only if called. What does he mean? 回答1: Member functions for class templates are only actually generated if they are used. This is an important part of templates

How can I add member functions in built-in classes in c++?

℡╲_俬逩灬. 提交于 2020-08-23 07:03:06
问题 I want to add a new member function "charReplace" to the string class. The function will replace all the occurances of one character with another character. So I prepared a sample code. #include <iostream> #include <string> std::string string::charReplace(char c1, char c2) { //error in this line while(this->find(c1) != std::string::npos) { int c1pos = this->find(c1); //find the position of c1 this->replace(c1pos, 1, c2); //replace c1 with c2 } return *this; } int main() { std::string s =

Why can't I mark this member function as const?

余生长醉 提交于 2020-07-03 19:24:14
问题 When I try to compile this short program: #include <iostream> class Foo { public: friend int getX() const; private: int x; }; int Foo::getX() const { return this->x; } int main() { Foo foo; std::cout << foo.getX() << std::endl; } I get these errors: C:\>gcc test.cpp test.cpp:6:23: error: non-member function 'int getX()' cannot have cv-qualifier friend int getX() const; ^ test.cpp:12:17: error: no 'int Foo::getX() const' member function declared in cl ass 'Foo' int Foo::getX() const { return

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

与世无争的帅哥 提交于 2020-05-28 13:48:02
问题 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 undefined reference (linker error) const char* asd = "ads"; c.f( asd ); // works as expected, NO errors return 0; } This is completely fine, right? I started doubting this, because I just ran over the specialization of '...' after instantiation error, which was new to me.

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

好久不见. 提交于 2020-05-28 13:47:59
问题 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 undefined reference (linker error) const char* asd = "ads"; c.f( asd ); // works as expected, NO errors return 0; } This is completely fine, right? I started doubting this, because I just ran over the specialization of '...' after instantiation error, which was new to me.

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

陌路散爱 提交于 2020-02-27 23:35:25
问题 I have the following code base: template <typename Type> class SomeClass { public: template <typename ReturnType, typename... Params> void register_function(const std::pair<std::string, ReturnType (Type::*)(Params...)> fct) { auto f = [fct](Params... params) -> ReturnType { return (Type().*fct.second)(std::ref(params)...); } // ... } }; This works when I pass a pointer to a member-function (non-const). However, if I want to pass a pointer to a const member-function, it results in a compile