Solved

operator overloading in Constructors

Posted on 2000-03-27
5
353 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 50 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

912 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now