?
Solved

operator overloading in Constructors

Posted on 2000-03-27
5
Medium Priority
?
383 Views
Last Modified: 2010-04-02
How do I use operator overloading in a Constructor ?
Pl. explain me with an example .
thanx.
0
Comment
Question by:gshankar
5 Comments
 

Accepted Solution

by:
YuraPhink earned 150 total points
ID: 2663067
What do you mean in a Constructor???

simple example of operator overloading:

class A
{
   int aa;
   public:
     
      A(int i=0):aa(i)
      {}
     
      A& operator =(const A &OPR)
      {
          aa=OPR.aa;
         
          return *this;
      }
};

void main()
{
   A cls1,cls2(5);

   cls1=cls2;   //it uses op. =
                //now cls1.aa=cls2.aa=5
/*
   also you can do:
   
   A cls1,cls2(5),cls3(7);

   cls1=cls2=cls3; //this will do:
                   //cls1.aa=cls2.aa=cls3.aa=7
*/

Thats all,
Hope it will help.
}
0
 

Expert Comment

by:YuraPhink
ID: 2663072
OK,
if you mean using operators (which you already defined in the class) in the constructor, then use them as well as in every other function (like in main).
0
 

Expert Comment

by:kaushikg
ID: 2663192
Can u give an example how you want to was in ctor??
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2663251
Do you mean conversion?

struct A
{
   int i;  
};

struct B
{
   int j;
   B() : j(0) {}
   B(const A& a) : j(a.i) {}
};


void f(const B& b)
{}

void g()
{
   A a;
   f(a);
}
0
 

Author Comment

by:gshankar
ID: 2663622
Yes . Iam convinced with YuraPhink's clue.
That cleared my doubt.
Thanks.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question