473,385 Members | 1,645 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Overloaded typecast operators and copy constructors

I have a few questions regarding overloaded typecast
operators and copy constructors that I would like an
answer for. Thanks in advance.

Masood

(1) In some examples that I have seen pertaining to
casting class A to class B, the implementation of the
overloaded typecast operator does something like this:

A::operator B(...)
{
B localCopy(...);
// perhaps some attributes of the "this" object
// are passed to the constructor

return localCopy;
}

Is this correct? Aren't we returning a local copy of
B?
(2) It appears that the following syntaxes are
equivalent:
[Here b is an instance of Class Y and Class Y has an
overloaded typecast operator for Class X]
(a) X c = (X)b; // Typecast style
(b) X c = X(b); // Function call style

Is this correct?
(3) I am surprised that the typecasting to self type is
allowed. If we have a class that supports both a copy
constructor with one argument and an overloaded
typecast operator to its own type (for whatever
reason), it appears that they will have identical
calling syntaxes.

Is this correct? If this is correct, how does the
compiler remove the ambiguity.

I have a small coding sample that illustrates my
doubts.
/////////////////////////////////////////////////////////////
#include <string.h>
#include <iostream.h>

class First
{
int value;
public:
First(int i = 0)
{
value = i;
}

operator int() // typecast to integer
{
return value;
}
};
class Second
{
int data;
char label[100];

public:
Second(int i = 0, char* str = "???")
{
data = i;
strcpy(label, str);
}

Second(const Second& obj) // Copy constructor
{
cout << "Copy constructor called\n";
data = obj.data;
strcpy(label, obj.label);
}

operator char*() // typecast to char*
{
return label;
}

operator First() // typecast to First
{
First f(data);
return f;
}

operator Second() // typecast to Self-type
{
cout << "Typecast operator to self called\n";
Second s(data, label);
return s;
}
};
int
main()
{
First first_a(56);

cout << "It is " << (int)first_a << endl;

Second second_b(98, "Howdy Doody");

First first_c = (First)second_b; // typecast style conversion
First first_d = First(second_b); // function call style conversion

cout << "ONE: It is " << (int)first_c
<< " and " << (char *)second_b << endl;
cout << "TWO: It is " << (int)first_d
<< " and " << (char *)second_b << endl;

Second second_e(second_b);
// What got called ---- the overloaded typecast
// self-type or the copy constructor?

return 0;
}

Jul 23 '05 #1
1 2199
<ma**********@lycos.com> wrote...
I have a few questions regarding overloaded typecast
operators and copy constructors that I would like an
answer for. Thanks in advance.

Masood

(1) In some examples that I have seen pertaining to
casting class A to class B, the implementation of the
overloaded typecast operator does something like this:

A::operator B(...)
{
B localCopy(...);
// perhaps some attributes of the "this" object
// are passed to the constructor

return localCopy;
}

Is this correct? Aren't we returning a local copy of
B?
It is correct. We're returning a copy of 'localCopy'.
(2) It appears that the following syntaxes are
equivalent:
[Here b is an instance of Class Y and Class Y has an
overloaded typecast operator for Class X]
(a) X c = (X)b; // Typecast style
(b) X c = X(b); // Function call style

Is this correct?
Depends on what 'X' is like. If it has a constructor that
accepts 'Y', then no, it's not correct.
(3) I am surprised that the typecasting to self type is
allowed.
You mean

struct A {
operator A();
};

?
If we have a class that supports both a copy
constructor with one argument and an overloaded
typecast operator to its own type (for whatever
reason), it appears that they will have identical
calling syntaxes.
It does, doesn't it?
Is this correct? If this is correct, how does the
compiler remove the ambiguity.
The conversion to the same object type is never used
directly. It can only be reached through a virtual
function call. See 12.3.2/1.

I have a small coding sample that illustrates my
doubts.
/////////////////////////////////////////////////////////////
#include <string.h>
#include <iostream.h>
#include <iostream>
using std::cout;
using std::endl;

class First
{
int value;
public:
First(int i = 0)
{
value = i;
}

operator int() // typecast to integer
{
return value;
}
};
class Second
{
int data;
char label[100];

public:
Second(int i = 0, char* str = "???")
Second(int i = 0, char const* str = "???")
{
data = i;
strcpy(label, str);
strncpy(label, str, 99);
label[99] = 0;
}

Second(const Second& obj) // Copy constructor
{
cout << "Copy constructor called\n";
data = obj.data;
strcpy(label, obj.label);
}

operator char*() // typecast to char*
{
return label;
}

operator First() // typecast to First
{
First f(data);
return f;
}

operator Second() // typecast to Self-type
{
cout << "Typecast operator to self called\n";
Second s(data, label);
return s;
}
};
int
main()
{
First first_a(56);

cout << "It is " << (int)first_a << endl;

Second second_b(98, "Howdy Doody");

First first_c = (First)second_b; // typecast style conversion
First first_d = First(second_b); // function call style conversion

cout << "ONE: It is " << (int)first_c
<< " and " << (char *)second_b << endl;
cout << "TWO: It is " << (int)first_d
<< " and " << (char *)second_b << endl;

Second second_e(second_b);
// What got called ---- the overloaded typecast
// self-type or the copy constructor?
The copy constructor.

return 0;
}

Jul 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Nitin Bhardwaj | last post by:
Thanx in advance for the response... I wanna enquire ( as it is asked many a times in Interviews that i face as an Engg PostGraduate ) about the overloading capability of the C++ Language. ...
5
by: Andy Jarrell | last post by:
I'm trying to inherit from a specific class that has an overloaded operator. The problem I'm getting is that certain overloaded operators don't seem to come with the inheritance. For example: ...
3
by: N4M | last post by:
Dear, I have problems with overloaded operators ++() and --(). MSVC++ 6.0 compiler gives errors, one is shown as below: " c:\data\c++\mygraphs\graph.h(182) : error C2555: 'CGraphNodeIter::++' :...
4
by: August1 | last post by:
I've written an interface and implementation file along with a client source file that allows the use of an overloaded subtraction operator. However, when using the program, I'm running into a...
14
by: ambar.shome | last post by:
Hi, As you know there are few operators in C++ which cant be overloaded. They are: .., .*, ::, ?: , new , delete , sizeof , typeid , static_casr , dynamic_cast , const_cast ,...
5
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
3
by: ryan.gilfether | last post by:
I have a problem that I have been fighting for a while and haven't found a good solution for. Forgive me, but my C++ is really rusty. I have a custom config file class: class ConfigFileValue...
3
by: iluvatar | last post by:
Hi all. I have written a 3d-vector class (for 3-dimensional space) and I have overloaded the arihtmetic operators like +, +=, * and so on. Also, the constructor works with doubles and has...
5
by: sam_cit | last post by:
Hi Everyone, I was just wondering, about the overloaded assignment operator for user defined objects. It is used to make sure that the following works properly, obj1 = obj; so the...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.