Search Results

Search found 3956 results on 159 pages for 'constructor overloading'.

Page 2/159 | < Previous Page | 1 2 3 4 5 6 7 8 9 10 11 12  | Next Page >

  • Default values on arguments in C functions and function overloading in C

    - by inquam
    Converting a C++ lib to ANSI C and it seems like though ANSI C doesn't support default values for function variables or am I mistaken? What I want is something like int funcName(int foo, bar* = NULL); Also, is function overloading possible in ANSI C? Would need const char* foo_property(foo_t* /* this */, int /* property_number*/); const char* foo_property(foo_t* /* this */, const char* /* key */, int /* iter */); Could of course just name them differently but being used to C++ I kinda used to function overloading.

    Read the article

  • Overloading methods that do logically different things, does this break any major principles?

    - by siva.k
    This is something that's been bugging me for a bit now. In some cases you see code that is a series of overloads, but when you look at the actual implementation you realize they do logically different things. However writing them as overloads allows the caller to ignore this and get the same end result. But would it be more sound to name the methods more explicitly then to write them as overloads? public void LoadWords(string filePath) { var lines = File.ReadAllLines(filePath).ToList(); LoadWords(lines); } public void LoadWords(IEnumerable<string> words) { // loads words into a List<string> based on some filters } Would these methods better serve future developers to be named as LoadWordsFromFile() and LoadWordsFromEnumerable()? It seems unnecessary to me, but if that is better what programming principle would apply here? On the flip side it'd make it so you didn't need to read the signatures to see exactly how you can load the words, which as Uncle Bob says would be a double take. But in general is this type of overloading to be avoided then?

    Read the article

  • Private constructor and public parameter constructor -C#

    - by Amutha
    I heard that private constructor prevent object creation from outside world. When i have a code public class Product { public string Name { get;set;} public double Price {get;set;} Product() { } public Product(string _name,double _price) { } } here still i can declare public constructor(parameter),won't it spoil the purpose of private constructor? When do we need both private and public constructor(parameter) in code? I need detailed explanation please.

    Read the article

  • Can overloading is possible with two version of function, constant member function and function with

    - by GG
    Hello, I just came across various overloading methods like type of parameter passed, varying number of parameters, return type etc. I just want to know that can I overload a function with following two version //function which can modify member String& MyClass::doSomething(); //constant member function String& MyClass::doSomething() const; Please let me know the reason behind it. Thanks, GG

    Read the article

  • function overloading in C

    - by FL4SOF
    Is there any way to achieve function overloading in C? I am looking at simple functions to be overloaded like foo (int a) foo (char b) foo (float c , int d) I think there is no straight forward way, looking for workarounds if any?

    Read the article

  • Partial overriding in Java (or dynamic overriding while overloading)

    - by Lie Ryan
    If I have a parent-child that defines some method .foo() like this: class Parent { public void foo(Parent arg) { System.out.println("foo in Function"); } } class Child extends Parent { public void foo(Child arg) { System.out.println("foo in ChildFunction"); } } When I called them like this: Child f = new Child(); Parent g = f; f.foo(new Parent()); f.foo(new Child()); g.foo(new Parent()); g.foo(new Child()); the output is: foo in Parent foo in Child foo in Parent foo in Parent But, I want this output: foo in Parent foo in Child foo in Parent foo in Child I have a Child class that extends Parent class. In the Child class, I want to "partially override" the Parent's foo(), that is, if the argument arg's type is Child then Child's foo() is called instead of Parent's foo(). That works Ok when I called f.foo(...) as a Child; but if I refer to it from its Parent alias like in g.foo(...) then the Parent's foo(..) get called irrespective of the type of arg. As I understand it, what I'm expecting doesn't happen because method overloading in Java is early binding (i.e. resolved statically at compile time) while method overriding is late binding (i.e. resolved dynamically at compile time) and since I defined a function with a technically different argument type, I'm technically overloading the Parent's class definition with a distinct definition, not overriding it. But what I want to do is conceptually "partially overriding" when .foo()'s argument is a subclass of the parent's foo()'s argument. I know I can define a bucket override foo(Parent arg) in Child that checks whether arg's actual type is Parent or Child and pass it properly, but if I have twenty Child, that would be lots of duplication of type-unsafe code. In my actual code, Parent is an abstract class named "Function" that simply throws NotImplementedException(). The children includes "Polynomial", "Logarithmic", etc and .foo() includes things like Child.add(Child), Child.intersectionsWith(Child), etc. Not all combination of Child.foo(OtherChild) are solvable and in fact not even all Child.foo(Child) is solvable. So I'm best left with defining everything undefined (i.e. throwing NotImplementedException) then defines only those that can be defined. So the question is: Is there any way to override only part the parent's foo()? Or is there a better way to do what I want to do?

    Read the article

  • Function overloading by return type?

    - by dsimcha
    Why don't more mainstream statically typed languages support function/method overloading by return type? I can't think of any that do. It seems no less useful or reasonable than supporting overload by parameter type. How come it's so much less popular?

    Read the article

  • Operator Overloading in C

    - by Leif Andersen
    In C++, I can change the operator on a specific class by doing something like this: MyClass::operator==/*Or some other operator such as =, >, etc.*/(Const MyClass rhs) { /* Do Stuff*/; } But with there being no classes (built in by default) in C. So, how could I do operator overloading for just general functions? For example, if I remember correctly, importing stdlib.h gives you the - operator, which is just syntactic sugar for (*strcut_name).struct_element. So how can I do this in C? Thank you.

    Read the article

  • Overloading assignment operator in C++

    - by jasonline
    As I've understand, when overloading operator=, the return value should should be a non-const reference. A& A::operator=( const A& ) { // check for self-assignment, do assignment return *this; } It is non-const to allow non-const member functions to be called in cases like: ( a = b ).f(); But why should it return a reference? In what instance will it give a problem if the return value is not declared a reference, let's say return by value?

    Read the article

  • Operator overloading in C++

    - by user265260
    If you overload - like operator-(), it is to be used to the left of the object, however overloading () like operator()() it is used to the right of the object. How do we know which operator is to be used on the left and which ones to be used on the right?

    Read the article

  • Simplifying const Overloading?

    - by templatetypedef
    Hello all- I've been teaching a C++ programming class for many years now and one of the trickiest things to explain to students is const overloading. I commonly use the example of a vector-like class and its operator[] function: template <typename T> class Vector { public: T& operator[] (size_t index); const T& operator[] (size_t index) const; }; I have little to no trouble explaining why it is that two versions of the operator[] function are needed, but in trying to explain how to unify the two implementations together I often find myself wasting a lot of time with language arcana. The problem is that the only good, reliable way that I know how to implement one of these functions in terms of the other is with the const_cast/static_cast trick: template <typename T> const T& Vector<T>::operator[] (size_t index) const { /* ... your implementation here ... */ } template <typename T> T& Vector<T>::operator[] (size_t index) { return const_cast<T&>(static_cast<const Vector&>(*this)[index]); } The problem with this setup is that it's extremely tricky to explain and not at all intuitively obvious. When you explain it as "cast to const, then call the const version, then strip off constness" it's a little easier to understand, but the actual syntax is frightening,. Explaining what const_cast is, why it's appropriate here, and why it's almost universally inappropriate elsewhere usually takes me five to ten minutes of lecture time, and making sense of this whole expression often requires more effort than the difference between const T* and T* const. I feel that students need to know about const-overloading and how to do it without needlessly duplicating the code in the two functions, but this trick seems a bit excessive in an introductory C++ programming course. My question is this - is there a simpler way to implement const-overloaded functions in terms of one another? Or is there a simpler way of explaining this existing trick to students? Thanks so much!

    Read the article

  • operator overloading c++

    - by segfault
    When overloading operators, is it necessary to overload = <= and !=? It seems like it would be smart for c++ to call !operator= for !=, ! for operator<= and !< for operator=. Is that the case, or is it necessary to overload every function?

    Read the article

  • Java overloading and overriding

    - by Padmanabh
    We always say that method overloading is static polymorphism and overriding is runtime polymorphism. What exactly do we mean by static here? Is the call to a method resolved on compiling the code? So whats the difference between normal method call and calling a final method? Which one is linked at compile time?

    Read the article

  • typedef and operator overloading in C

    - by jocapco
    Suppose I typedef an integer or integer array or any known type: typedef int int2 Then I overload operator * for int2 pairs, now if I initialize variables a and b as int. Then will my * between a and b be the overloaded * ? How do I achieve overloading an int and yet also use * for int the way they are. Should I create a new type?

    Read the article

  • Can I have conditional construction of classes when using IoC.Resolve ?

    - by Corpsekicker
    I have a service class which has overloaded constructors. One constructor has 5 parameters and the other has 4. Before I call, var service = IoC.Resolve<IService>(); I want to do a test and based on the result of this test, resolve service using a specific constructor. In other words, bool testPassed = CheckCertainConditions(); if (testPassed) { //Resolve service using 5 paramater constructor } else { //Resolve service using 4 parameter constructor //If I use 5 parameter constructor under these conditions I will have epic fail. } Is there a way I can specify which one I want to use?

    Read the article

  • C# String Operator Overloading

    - by ScottSEA
    G'Day Mates - What is the right way (excluding the argument of whether it is advisable) to overload the string operators <, , <= and = ? I've tried it five ways to Sunday and I get various errors - my best shot was declaring a partial class and overloading from there, but it won't work for some reason. namespace System { public partial class String { public static Boolean operator <(String a, String b) { return a.CompareTo(b) < 0; } public static Boolean operator >(String a, String b) { return a.CompareTo(b) > 0; } } }

    Read the article

  • Operator Overloading with C# Extension Methods

    - by Blinky
    I'm attempting to use extension methods to add an operater overload to the C# StringBuilder class. Specifically, given StringBuilder sb, I'd like sb += "text" to become equivalent to sb.Append("text"); Here's the syntax for creating an extension method for StringBuilder: public static class sbExtensions { public static StringBuilder blah(this StringBuilder sb) { return sb; } } It successfully adds the "blah" extension method to the StringBuilder. Unfortunately, operator overloading does not seem to work: public static class sbExtensions { public static StringBuilder operator +(this StringBuilder sb, string s) { return sb.Append(s); } } Among other issues, the keyword 'this' is not allowed in this context. Are adding operator overloads via extension methods possible? If so, what's the proper way to go about it?

    Read the article

  • Opt-out of copy constructor

    - by sheepsimulator
    This might be a silly question, but... I've been writing a number of classes that utilize non-copyable members. These classes are never initialized via the copy constructor in my source. When I try to compile without supplying my own copy-constructor, g++ throws out many errors about how it can't build a default copy constructor, due to the non-copyable member objects. Is there a way to tell the compiler to just not give me a copy constructor?

    Read the article

  • bug with varargs and overloading?

    - by pstanton
    There seems to be a bug in the Java varargs implementation. Java can't distinguish the appropriate type when a method is overloaded with different types of vararg parameters. It gives me an error The method ... is ambiguous for the type ... Consider the following code: public class Test { public static void main(String[] args) throws Throwable { doit(new int[]{1, 2}); // <- no problem doit(new double[]{1.2, 2.2}); // <- no problem doit(1.2f, 2.2f); // <- no problem doit(1.2d, 2.2d); // <- no problem doit(1, 2); // <- The method doit(double[]) is ambiguous for the type Test } public static void doit(double... ds) { System.out.println("doubles"); } public static void doit(int... is) { System.out.println("ints"); } } the docs say: "Generally speaking, you should not overload a varargs method, or it will be difficult for programmers to figure out which overloading gets called." however they don't mention this error, and it's not the programmers that are finding it difficult, it's the compiler. thoughts?

    Read the article

  • Overloading the QDataStream << and >> operators for a user-defined type

    - by Alex Wood
    I have a an object I'd like to be able to read and write to/from a QDataStream. The header is as follows: class Compound { public: Compound(QString, QPixmap*, Ui::MainWindow*); void saveCurrentInfo(); void restoreSavedInfo(QGraphicsScene*); void setImage(QPixmap*); QString getName(); private: QString name, homeNotes, addNotes, expText; Ui::MainWindow *gui; QPixmap *image; struct NMRdata { QString hnmrText, cnmrText, hn_nmrText, hn_nmrNucl, notes; int hnmrFreqIndex, cnmrFreqIndex, hn_nmrFreqIndex, hnmrSolvIndex, cnmrSolvIndex, hn_nmrSolvIndex; }*nmr_data; struct IRdata { QString uvConc, lowResMethod, irText, uvText, lowResText, highResText, highResCalc, highResFnd, highResFrmla, notes; int irSolvIndex, uvSolvIndex; }*ir_data; struct PhysicalData { QString mpEdit, bpEdit, mpParensEdit, bpParensEdit, rfEdit, phyText, optAlpha, optConc, elemText, elemFrmla, notes; int phySolvIndex, optSolvIndex; }*physical_data; }; For all intensive purposes, the class just serves as an abstraction for a handful of QStrings and a QPixmap. Ideally, I would be able to write a QList to a QDataStream but I'm not exactly sure how to go about doing this. If operator overloading is a suitable solution, would writing code like friend QDataStream& operator << (QDataStream&,Compound) { ... } be a potential solution? I'm very open to suggestions! Please let me know if any further clarification is needed.

    Read the article

  • Implementing Operator Overloading with Logarithms in C++

    - by Jacob Relkin
    Hello my friends, I'm having some issues with implementing a logarithm class with operator overloading in C++. My first goal is how I would implement the changeBase method, I've been having a tough time wrapping my head around it. My secoond goal is to be able to perform an operation where the left operand is a double and the right operand is a logarithm object. Here's a snippet of my log class: // coefficient: double // base: unsigned int // number: double class _log { double coefficient, number; unsigned int base; public: _log() { base = rand(); coefficient = rand(); number = rand(); } _log operator+ ( const double b ) const; _log operator* ( const double b ) const; _log operator- ( const double b ) const; _log operator/ ( const double b ) const; _log operator<< ( const _log &b ); double getValue() const; bool changeBase( unsigned int base ); }; You guys are awesome, thank you for your time.

    Read the article

  • What is the rationale to not allow overloading of C++ conversions operator with non-member function

    - by Vicente Botet Escriba
    C++0x has added explicit conversion operators, but they must always be defined as members of the Source class. The same applies to the assignment operator, it must be defined on the Target class. When the Source and Target classes of the needed conversion are independent of each other, neither the Source can define a conversion operator, neither the Target can define a constructor from a Source. Usually we get it by defining a specific function such as Target ConvertToTarget(Source& v); If C++0x allowed to overload conversion operator by non member functions we could for example define the conversion implicitly or explicitly between unrelated types. template < typename To, typename From > operator To(const From& val); For example we could specialize the conversion from chrono::time_point to posix_time::ptime as follows template < class Clock, class Duration> operator boost::posix_time::ptime( const boost::chrono::time_point<Clock, Duration>& from) { using namespace boost; typedef chrono::time_point<Clock, Duration> time_point_t; typedef chrono::nanoseconds duration_t; typedef duration_t::rep rep_t; rep_t d = chrono::duration_cast<duration_t>( from.time_since_epoch()).count(); rep_t sec = d/1000000000; rep_t nsec = d%1000000000; return posix_time::from_time_t(0)+ posix_time::seconds(static_cast<long>(sec))+ posix_time::nanoseconds(nsec); } And use the conversion as any other conversion. For a more complete description of the problem, see here or on my Boost.Conversion library.. So the question is: What is the rationale to non allow overloading of C++ conversions operator with non-member functions?

    Read the article

< Previous Page | 1 2 3 4 5 6 7 8 9 10 11 12  | Next Page >