non-member-functions

In which file do we put non-member function in C++?

给你一囗甜甜゛ 提交于 2021-02-17 05:14:28
问题 What is the normal practice when it comes to non-member function in C++? Do we put them in main.cpp or header file or class implementation file, or do we make a separate .cpp file for it? If the normal practice is to make a separate file, then where do we put the non-member function header(prototype)? Does it only go in main.cpp or in both of them? 回答1: I would say you should not treat non-member functions differently to classes and member functions and other symbols. You should create a

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 member function address are so far away from free functions?

社会主义新天地 提交于 2020-02-02 13:07:31
问题 Taking this example: https://godbolt.org/z/gHqCSA #include<iostream> template<typename Return, typename... Args> std::ostream& operator <<(std::ostream& os, Return(*p)(Args...) ) { return os << (void*)p; } template <typename ClassType, typename Return, typename... Args> std::ostream& operator <<(std::ostream& os, Return (ClassType::*p)(Args...) ) { unsigned char* internal_representation = reinterpret_cast<unsigned char*>(&p); os << "0x" << std::hex; for(int i = 0; i < sizeof p; i++) { os <<

Support of std::cbegin() in C++14

雨燕双飞 提交于 2020-01-03 13:35:45
问题 Item 13 from Scott Mayers' "Effective Modern C++" states to prefer const_iterators over iterators. I agree but I also want to use non-member functions rather than member functions. According to the book there should be a non-member function std::cbegin() and std::cend() in C++14. To make use of this functions I just installed gcc version 4.9.2 and compiled with the flag -std=c++14 . It seems to compile until I try to use std::cbegin() . I start searching for the support for this function but

c++ create, assign and compare a new variable to two object inside an Operator Overloaded function.

女生的网名这么多〃 提交于 2020-01-03 05:16:19
问题 The assignment: Implement an Alien class using a provided Alien.h file. An alien, in this scenario is described in terms of his/her height, weight, and gender. To compare two aliens, you use the following equation to determine the alien’s statusPoints value: statusPoints = weight * height * genderValue where genderValue is 2 if the alien is male, and 3 if the alien is female. The status points should be calculated when needed, not kept as a data member. This avoids so-called stale data, in

Can refactoring an overloaded operator into a non-member function break any code?

こ雲淡風輕ζ 提交于 2019-12-30 08:24:24
问题 Consider a legacy class template with overloaded addition operators += and + template<class T> class X { public: X() = default; /* implicict */ X(T v): val(v) {} X<T>& operator+=(X<T> const& rhs) { val += rhs.val; return *this; } X<T> operator+ (X<T> const& rhs) const { return X<T>(*this) += rhs; } private: T val; }; Upon code review, it is observed that + is implementable in terms of += , so why not make it a non-member (and have guaranteed symmetry for left and right arguments)? template

Accessing a C++ non-member function from C# via reflection

本小妞迷上赌 提交于 2019-12-24 10:59:10
问题 I need to gain some run-time information about a C++ program, which is kinda difficult due to C++ not offering some sophisticated reflection mechanism. Now, my approach is to compile the C++ code using /clr and to reflect over the resulting assembly from C# (simply because I like that language more than C++). While this is all turning out more or less fine, I'm now stuck at a point where I need to actually run the program by calling its main method. Which is kind of frustrating considering

Class VERSUS namespace, OR class AND namespace? [closed]

家住魔仙堡 提交于 2019-12-21 07:27:14
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . Both Class and Namespace? This question is about a pattern that I am seeing myself use more and more: Having both a class and a

Partial template specialization of free functions - best practices

十年热恋 提交于 2019-12-20 12:16:04
问题 As most C++ programmers should know, partial template specialization of free functions is disallowed. For example, the following is illegal C++: template <class T, int N> T mul(const T& x) { return x * N; } template <class T> T mul<T, 0>(const T& x) { return T(0); } // error: function template partial specialization ‘mul<T, 0>’ is not allowed However, partial template specialization of classes/structs is allowed, and can be exploited to mimic the functionality of partial template

Invalid use of 'this' in non-member function

↘锁芯ラ 提交于 2019-12-18 19:19:09
问题 I had working on a class and started writing everything in the same .cpp file. However, after a while I could see the class getting bigger and bigger so I decided to split it into a .h and a .cpp file. gaussian.h file: class Gaussian{ private: double mean; double standardDeviation; double variance; double precision; double precisionMean; public: Gaussian(double, double); ~Gaussian(); double normalizationConstant(double); Gaussian fromPrecisionMean(double, double); Gaussian operator *