operator overloading in Constructors

Posted on 2000-03-27
Last Modified: 2010-04-02
How do I use operator overloading in a Constructor ?
Pl. explain me with an example .
Question by:gshankar
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

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

simple example of operator overloading:

class A
   int aa;
      A(int i=0):aa(i)
      A& operator =(const A &OPR)
          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:

Thats all,
Hope it will help.

Expert Comment

ID: 2663072
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).

Expert Comment

ID: 2663192
Can u give an example how you want to was in ctor??

Expert Comment

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;

Author Comment

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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
  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 …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

730 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