I just want to know if C supports over loading?
As we use system functions like printf with different no of arguments.
Help me out
|
C doesn't have (to the best of my knowledge) overloading or templates, right? So how can a set of type-agnostic functions with the same name exist in plain ol' C? The ... |
|
John Doe Hi, I am trying to transform a class with some time consuming operation by adding a thread. To be able to pass data to thread I have declared a class ThreadParam as shown below : class CProvisioning { public: struct ThreadParam { ThreadParam(CProvisioning* a_pThis, const CString& a_strConfig, CString& a_strXmlOut): pThis(a_pThis), strConfig( a_strConfig ), strOut( a_strXmlOut ) {} CProvisioning* pThis; ... |
|
Nikhil.S.Ketkar@gmail.com Hi, Does the following construct qualify as overloading on return type ? If not, in what way ? I overloaded the type conversion operator and used pointers to member functions. Its pretty straightforward but I am sure I have missed as this is not supposed to be possible. Am I misunderstanding overloading ? Thanks, Nikhil // foo() overloads on both ... |
Hi, I would like to merge two class which have got 90% of same source code. But I don't know what to do. class A { int putLightOn () { switch(status) { case iAmOff : print "I am off" ; print "put me on!!!\n" ; } } } class B { int putLightOn () { switch(status) { case iAmOff : print ... |
|
> int main( void ) { bar( baz( ), baz( ) ); // error: ambiguous overload The code generated here would be: template |
xtrigger303@gmail.com Hi to all, I was reading Mr. Alexandrescu's mojo article and I've a hard time understanding the following. Let's suppose I have: //code struct A {}; struct B : A {}; struct C { operator A() const { return A(); } operator B() { return B(); } }; void F( A const & ) {} void F( B const & ... |
Hi all, I have a set of classes which implement the virtual constructor idiom. > I had a slicing problem which resulted when I forgot to override the clone() function in a derived class. > Is there something (other than documentation) that I can do to prevent this from happening again? > |
Sorry for this stupid question, but i am lost. If i write an stringlib with += overload operators (no i do not, but my thing is much more complicated) , and i have to precalculate the strlen() -- as seen in the example here How do i solve this ? struct myStr { private: unsigned len; unsigned size; char string[100]; public: ... |
I'm not overloading return types so much as defining what happens when I cast the instance of Message to another datatype. Something to add to my original post- if I explicitly cast msg to const char *, it compiles. But for consistency and code length I would like to not have to do that. |
Do keep in mind that a vector can have any dimensions you need. For example, if there are 12 ints in the vector, you could say that it is an array of [12] or an array [6][2] or an array [4][3] or an array [2][2][3]. You manage the dimensions by using iterators. You just need the one vector in the base ... |
#include using namespace std; class Integer{ int i; public: Integer(int ii):i(ii){} const Integer operator+(const Integer& rv){ cout<<"1-operator+"< |
RobertEstelle@gmail.com Hello, I've been banging my head against this problem for a couple hours now to no avail. I'm trying to make a descendant of a class that has overloaded methods with variable argument lists. The problem is, when my descendant class does the same, function resolution seems to stop there without looking at the base class. This happens both with ... |
Latina Hi, I am doing a program using overloded operators but I am getting some error, Can some one help please. Here is my code: #include #include #include using namespace std; class Date { private: int mon; int day; int year; string m; public: friend istream& operator>>(istream& is, Date& d1); friend ostream &operator<<(ostream &os, const Date &d1); int operator-(Date); bool operator>(Date); ... |
On 12 Oct, 22:18, Dan Noland Thanks, I just found a similar explanation in section 19.3.1 of Lippman and Lajoie. It seems strange that the shadowing is done by function name rather than function prototype, but this works. Do any language/compiler gurus know why this is the case? > YT, Dan Nolandhttp://nolandda.org/ It's a safety measure against very difficult ... |
P: n/a Colonel It seems that the problems have something to do with the overloading of istream operator ">>", but I just can't find the exact problem. // the declaration friend std::istream & operator>(std::istream & in, const Complex & a); // the methods correspond to the friend std::istream & operator>(std::istream & in, const Complex & a) { std::cout << "real: "; ... |
Adam Nielsen Hi everyone, I've run into yet another quirk with templates, which IMHO is a somewhat limiting feature of the language. It seems that if you inherit multiple classes, and those classes have two functions with the same name, this is an error - even when the functions take different parameters, and they would happily exist as overloaded functions if ... |
Hi, with this code: class Base { public: virtual int Simple(int i, int j); virtual int Simple(int i); virtual ~Base(); // make compiler happy }; class Derived : public Base { public: virtual int Simple(int i); }; int main(int argc, char* argv[]) { Derived b; b.Simple(1, 2); } Compiling with gcc 4.1.1 the call b.Simple(1,2) in main is not resolved, while ... |
Goran wrote: Is it possible to overload a member variable? Yes; they are just two different variables. The working model here is that each class gives its data members scope and a storage slot. Deriving a class opens a new scope and a new storage region. Activity in that scope will look-up identifiers within its scope first, so the situation is ... |
class Base { public: virtual void f(int) { std::cout<<"base.f(int)\n";}; virtual void f(std::complex) { std::cout<<"derived.f \n"; }; }; class Derived : public Base { public : virtual void f(double) {std::cout<<"base.f(double)\n";}; }; int main() { Base b; //Line 1 Derived d; //Line 2 Base* pb = new Derived; //Line 3 pb->f(1.0f); //Line 4 return 0; } the above program is calling f(int) version, ... |
On 13 Maj, 23:35, MathStuf error: call of overloaded `LogSetVar(const char[8], unsigned int&, const char[5], unsigned int&, const String&)' is ambiguous note: candidates are: void LogSetVar(const char*, unsigned int, const ... |
yashwant pinge wrote: #include using namespace std; > class base { public: void display() { } }; > class derived : public base { public: void display(int i ) { } }; > int main() { derived d; d.display(1); } > > can anyone tell me that display funcion in derived class is a overriding function or overloading function Neither. Overloading ... |
|
Hi all, I know it is possible to overload the operators and < in C++ but how can I do this. Assume I have a class Date with three int members, m_day, m_month and m_year. In the .cpp files I have defined the < and operators; bool Date::operator<(const Date& d) { return // this year < d year (m_year < d.m_year) ... |
So lets say I have this pretend function that compares a given serial number against the one true serial VALID_SERIAL: bool validateSerial(string serialNum) { if(serialNum == VALID_SERIAL) return true; else return false; } I want to add a function that does the same thing, but will also pad the given serialNum with a prefix of "0" if the original serialNum does ... |
Hi, I coded the following but It does not return what I expect, why? #include using namespace std; class Other { public: int i; Other(int x=1) { i = x; } Other *operator-() { return this;} Other &operator+ (Other t) { i += t.i; return *this; } Other &operator,(Other oth) { i = oth.i; return *this; } }; int main() ... |
On 6 Feb 2007 18:11:37 -0800, rohits123@gmail.com wrote: >I have an overload delete operator as below > >////////////////////////////////// >void operator delete(void* mem,int head_type) { > mmHead local_Head = CPRMemory::GetMemoryHead(head_type); > mmFree(&local_Head,(char *)mem); > CPRMemory::SetMemoryHeadAs(local_Head,head_type); >} >///////////////////// >void* operator new(size_t sz, int head_Type) { > char *mem; > mmHead local_Head = CPRMemory::GetMemoryHead(head_Type); > mem = mmAlloc(&local_Head,sz); > CPRMemory::SetMemoryHeadAs(local_Head,head_Type); > if(!mem) cout<<"Out of ... |
Chameleon What is better if you want upcasting in intermediate classes like below? Multiple Inheritance and Overloading or simply RTTI? RTTI wants time but MI and Overloading create big objects (because of virtual) and finally they need time too to access objects inside big object. (Examples looks complicated but are very simple) Paradigm #1: MI & Overloading ============================= //--------------- base classes ... |
P: n/a Huck Phin OK, so I have looked and looked for something similar to my problem, and I cannot find one. I am writing a program for a HugeInteger, and I am attempting to overload the preincrement, postincrement, predecrement, and postdecrement operators. I realize that they are really all the same, and if I get one of the four operators ... |
|
Hi everyone, Given the code below, can anyone tell me: a) Is this normal behaviour ? b) If it is, what is the reason behind it ? I would expect the A::method(int) to be inherited by B. Compiler: gcc 4.1, Linux Thanks, Mihai ----------------------- #include class A{ public: virtual void method(int); }; class B: public A{ public: virtual void method(); ... |
Ole Nielsby wrote: I'm puzzed by this: .... ... I stumbled on the problem by accident, not because i was trying to push the limits of C++. Yes, I am not sure why the C++ gods thought that it was a good idea to hide inherited functions with the same name. But it is so. However, the "using" keyword can "unhide" ... |
Jerry Coffin re: overloading subscript In article <1157866759.637466.246860@b28g2000cwb.googlegroups .com>, murali.salem@gmail.com says... hello everybody... > how can i overload the subscript operator with more than one dimension... like [][][].... You can't do it directly -- you can either 1) overload some other operator (most often ()), or 2) you can overload operator[] to return some type that itself overloads operator[], and so ... |
Let's assume I have a base class class X { }; and the the following classes inheriting class BX : public X { }; class AX : public X { }; and this class class MyClass { void DoSomething( AX* ); void DoSomething( BX* ); } and this code X* = ... MyClass c; c.DoSomething(X) Do I have to use a ... |
Hi all, I am facing a peculiar problem. I am using a DLL which overloads the global new and delete as it uses its own memory manager. I am linking (implicitly) to this DLL. I am using another static library which has my CPP classes. Say SomeClass. >From my executable when I do SomeClass *sc = new SomeClass() <---- the CRT ... |
hi I am trying to make some simple app to learn exception safety coding. I know that new and delete can throw bad_alloc but how can I force them to throw by a flag "change" at run time? I am overloading new, new[], delete and delete[], change is set to 1 then it calls the custom operators. If 0, it calls ... |
andrew browning I am getting an istream overloading error that looks like this: error: no match for 'operator>>' in 'ins >> target->abrowning_rational::Rational::numerator' Below is the .h file: #ifndef RATIONAL #define RATIONAL #include namespace abrowning_rational{ class Rational{ public: //CONSTRUCTOR for the rational class Rational(); Rational(int a, int b); //CONSTANT MEMBER FUNCTIONS for the Rational class int getNum () const {return numerator;} ... |
Sunil Varma wrote:[color=blue] > No, it's not possible to overload functions based on its return type.[/color] There is one thing permitted by the language which resembles this -- covariant return types. It has more to do with overriding, though. In the case where a parent's member function is overridden, a "more specifically derived" (to coin a phrase?) return type -- that ... |
Uh, what is your question? Yes, overloaded functions are part of the C++ language. Yes, there is an algorithm for determining which overloaded function gets called, based on the arguments. The majority of cases, for properly designed functions, are simple and intuitive. Corner cases can get a bit abstruse -- if you're interested in the nitty gritty, google "Koenig lookup" (nods ... |
Nagaraj wrote:[color=blue] > Can we overload main() function?[/color] 3.6.1p2 "An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a return type of type int, but otherwise its type is implementation defined." 3.6.1p3 "The function main shall not be used (3.2) within a program. The linkage (3.5) of main is implementation defined. A program ... |
Code: bool Student::operator!() { if(!canRegisterMore()) return true; else return false; } bool Student::registerCourse(Course course) //thise function expline abilety of register course but you should //the 1 can register more is true and 2 add currentcridet and number of cridet<10 { if (canRegisterMore() &&(currentcredits+ course.getNumberOfCridet())<=10) { cout << "You can register course "< |
|
Thanks wschweit! My compiler still rejects the == operator until I add 'const' before the first open brace in the definition. I don't know what to return for the > , < comparisons, I can't just return true or false, because what if the elements are equal? So I tried returning an int, but I guess that's not right because I ... |
|
Quote: Originally Posted by miica thanks, and mitakeet, can you explain a bit about screen buffering? I don't know what's that. Under Windows, a specific window will receive a 'message' from the OS whenever it needs to be redrawn. Note that all the standard controls (edit controls, list boxes, buttons, combo boxes, etc) are just specialized forms of windows, generally child ... |
On first glance the one thing that jumps out at me are the entries from this series: if (sptr == "January") monthNumber = 1; sptr is a pointer to a character array. First, you should consider making that a const char *sptr instead of char *sptr, but that's minor here. The test fail, with the possible exception of an optimization that ... |
Code: #include using namespace std; class fraction { private: int numerator; int denominator; public: void assignFraction(int a, int b); int getNumerator() { return numerator; } int getDenominator () { return denominator; } void Input(istream& in); }; void fraction::assignFraction(int a, int b) { numerator = a; denominator = b; } ostream& operator << (ostream& out, fraction& s) { return out< |
|
This assignment is killing me...glad it's almost the last one. Here's my new weird problem. I'm getting the error message "date.cpp(82) : error C2666: '<<' : 14 overloads have similar conversions.." Here are my code snippets: Date.h Code: #ifndef DATE_H #define DATE_H #include #include using namespace std; class Date { ;// define terminal I/O overloads which print a nice ... |
|
|
My constructor accepts a const char* and assigns it to the private array. I want the person to be able to do X="String Literal" and have the string literal assigned to X. In the class I have: S operator= (const char*); and the definition is: S S::operator= (const char* p) { S temp(p); return temp; } If one does: X asdf("temp"); ... |
Overloading on return values Its common to wonder why just scopes and argument lists? Why not return values? It seems at first that it would make sense to also mangle the return value with the internal function name. Then you could overload on return values, as well: void f(); int f(); This works fine when the compiler can unequivocally determine the ... |
Hi, i am having trouble overloading the << operator for a class that I have written. Suppose my class is def as follows: Code: class Set { public: Set(int elem_num, int option) { for(int i=0; i |
|
Thanks for the info Andre and for fixing up the code. Unfortunately it was still a bit mangled, so I have updated the code just as a plain text paste - not pretty, but the preview looks like template params have been preserved etc. The target (object) pointer is stored internally in the Source as a void pointer (The source has ... |
|
@Optimus: x and y are the two operands of type Key for the == operator, so if one were to write the expression x == y what happens behing the scenes is that the function the_key() is called on both operands and the two values (here integers) are compared. Since this was probably an example on operator overloading they left out ... |
|