overloading

Compiler warning on “Effective C++”s method to avoid duplication in const and non-const member functions

社会主义新天地 提交于 2019-12-08 06:44:44
问题 This question has been updated. Please review the code. The following code was compiled with VC++ Nov 2012 CTP. Scott Meyers' book " Effective C++ " recommend that we should use the method of to avoid duplication in const and non-const member functions. However, the following code cause a warning (level 1). Because WDK build tool treats warnings as errors, so the following code cannot be compiled successfully. Is there other better method? struct A { int n; A(int n) : n(n) {} int Get() const

special method for an object to override tuple expansion?

*爱你&永不变心* 提交于 2019-12-08 06:13:09
问题 I will provide an example of the problem in question, in case the title was not clear enough. Let's say that I have a class Point(object) that represent 2d coordinates. Is it possible to create a "magic" method that will allow the following? x, y = point Maybe some hacks with iterators? 回答1: Just provide an __iter__ method. class Point(object): def __init__(self, x, y): self.x = x self.y = y def __iter__(self): yield self.x yield self.y p = Point(1, 2) x, y = p assert (1, 2) == (x, y) Be

accessing operator overloading of class which is wrapped by std::shared_ptr

孤者浪人 提交于 2019-12-08 03:36:57
问题 the idea is that I want a class which is wrapped by std::shared_ptr , can still be used just like they weren't a pointer, for example the operator= which was defined in my class can still be used after my class is wrapped by std::shared_ptr . for example template <class Ty> class shared_ptr_proxy : public std::shared_ptr<Ty> { public: template<class Other> shared_ptr_proxy& operator=(const Other& rhs) { (*this->get()) = rhs; return *this; } template<class Other> explicit shared_ptr_proxy

C++/CLI: Is overloading on return type only possible?

最后都变了- 提交于 2019-12-08 03:16:56
问题 If I understand well, in C#, it is possible to do public class X : ICloneable { public X Clone() { ... } object ICloneable.Clone() { return Clone(); } // This calls the above } according to this thread. This kind of overloading is forbidden in C++, since it only depends on the return type. Now, I would like to do this exact thing with ICloneable in C++/CLI. Is there a way ? 回答1: This type of overloading is allowed in C# not because of different return type, but because of explicit

Swift: Method overriding in parameterized class

限于喜欢 提交于 2019-12-08 01:57:30
问题 I'm very new to Swift but I have some experience with OO-programming. I've started to try and use parameterized classes in Swift and I have come across a strange design feature when overloading methods. If I define the following classes: class ParameterClassA { } class ParameterClassB: ParameterClassA { } class WorkingClassA<T: ParameterClassA> { func someFunction(param: T) -> Void { } } class WorkingClassB: WorkingClassA<ParameterClassB> { override func someFunction(param: ParameterClassA) {

Java: If I overwrite the .equals method, can I still test for reference equality with ==?

 ̄綄美尐妖づ 提交于 2019-12-08 01:36:26
问题 I have the following situation: I need to sort trees based by height, so I made the Tree's comparable using the height attribute. However, I was also told to overwrite the equals and hashCode methods to avoid unpredictable behaviour. Still, sometimes I may want to compare the references of the roots or something along those lines using ==. Is that still possible or does the == comparison call the equals method? 回答1: equals() is meant to compare an object with rules set by the programmer. In

use operator << to push std::strings in a vector

陌路散爱 提交于 2019-12-08 01:34:21
问题 How it possible to use operator<< to push string s into a vector . I searched a lot but only find stream examples. class CStringData { vector< string > myData; // ... // inline operator << ... ??? }; I want this to use as a simple elipsis (like void AddData(...) ) exchange for robust parameters. CStringData abc; abc << "Hello" << "World"; Is this possible at all ? 回答1: You can define operator<< as: class CStringData { vector< string > myData; public: CStringData & operator<<(std::string const

Ambiguous overload accessing argument-less template functions with variadic parameters

久未见 提交于 2019-12-08 01:06:49
问题 Yeah, the title can scare babies, but it's actually quite straightforward. I am trying to store a function pointer to a specialized template function, namely boost::make_shared (boost 1.41), as illustrated: boost::shared_ptr<int> (*pt2Function)() = boost::make_shared<int>; However, it won't compile (GCC 4.4.1) due to the fact that boost::make_shared has the following two specializations which the compiler can't tell apart in this context: template< class T > boost::shared_ptr< T > make_shared

Can “overloading” via FlexibleInstances return different types, or match on typeclasses?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-07 18:04:49
问题 I'm curious about what kind of "overloading" can be accomplished in Haskell's type classes via "FlexibleInstances". As a simple test, here is a case of an AdjusterType datatype. It defines an adjust operation that will add a different amount to its value based on whether it contains an Integer or a Double: {-# LANGUAGE FlexibleInstances #-} class Adjustable a where adjust :: a -> Double data AdjusterType a = Adjuster a deriving Show instance Adjustable (AdjusterType Integer) where adjust

Java Annotation Overloading?

微笑、不失礼 提交于 2019-12-07 17:32:15
问题 In my project, I have defined the an annotation similar to the following: (Omitting @Retention , @Target for brevity) public @interface DecaysTo { String[] value(); } Since originally writing it, our needs have changed and I have now defined an enum that I want to be able to use in place of the string: public enum Particle { ELECTRON, ANTIELECTRON, NEUTRINO, ANTINEUTRINO, ... } To avoid updating every instance of this annotation, I would like to be able to construct the annotation with either