473,387 Members | 1,464 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,387 software developers and data experts.

return parameter is not allowed?

I made CMyString class, and make operator=.
but in (operator=) function, there exists error.
================================================
class CMyString
{
private:
char *m_pStr; //dynamic array, changing the size
public:
CMyString();
CMyString(const CMyString &mystr);
CMyString(char str[]);
~CMyString();
CMyString& operator = (const CMyString &mystr);
};
CMyString::CMyString()
{
m_pStr = new char[1];
m_pStr[0] = NULL;

}
CMyString::CMyString(char str[])
{
int length=0;
length = strlen(str);
m_pStr = new char[length+1];
strcpy(m_pStr, str);
}
CMyString::CMyString(const CMyString &mystr)
{
int length=0;
length = strlen(mystr.m_pStr);
m_pStr = new char[length+1];
strcpy(m_pStr, mystr.m_pStr);
}
CMyString::~CMyString()
{
if(m_pStr!=NULL) {
delete [] m_pStr;
}
}
CMyString& CMyString::operator = (const CMyString &mystr)
{
return mystr;
}
==================================================
C:\Program Files\Microsoft Visual Studio\MyProjects\LearningPointer\1_pointer.cpp(70 ) : error C2440: 'return' : cannot convert from 'const class CMyString' to 'class CMyString &'
Conversion loses qualifiers

. ======================================
what is the problem? Is it related to constructor or destructor?
Can't I return parameter?
I greatly need your kind comment.
Sep 16 '08 #1
2 1994
JosAH
11,448 Expert 8TB
CMyString& CMyString::operator = (const CMyString &mystr)
{
return mystr;
}
==================================================
C:\Program Files\Microsoft Visual Studio\MyProjects\LearningPointer\1_pointer.cpp(70 ) : error C2440: 'return' : cannot convert from 'const class CMyString' to 'class CMyString &'
Conversion loses qualifiers

. ======================================
what is the problem? Is it related to constructor or destructor?
Can't I return parameter?
I greatly need your kind comment.
Your parameter is a const and you attempt to return a non-const reference;
you can't 'cast away the const'. You either make the return type a const as
well or leave out the const for the parameter.

kind regards,

Jos
Sep 16 '08 #2
newb16
687 512MB
C:\Program Files\Microsoft Visual Studio\MyProjects\LearningPointer\1_pointer.cpp(70 ) : error C2440: 'return' : cannot convert from 'const class CMyString' to 'class CMyString &'
Conversion loses qualifiers
You cant return const object where return type is not const, because you cant' assign const object to non-const. You need to copy data from members of argument to member of this and return this.
Sep 16 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Murat Tasan | last post by:
so i have a situation that i know cannot be solved directly, and i've already worked around it, but i was hoping to find an explanation as to why this behavior is as it is... i have an abstract...
2
by: Konstantin Zakharenko | last post by:
Hello, Our QA team have running a lot of test scripts (for automated regression testing), they run them on the different databases (Oracle/MS SQL). Several of those tests are dependent on the...
6
by: Tushar | last post by:
Is it possible to have a method that accepts XML document (including processing instruction at top and the root node) as a parameter? Does this parameter need to be defined as type string? I...
32
by: Panks | last post by:
Hey All Its one of basic questions? i read in a book "Thinking in C++" it says " Overloading solely on return value is a bit too subtle, and thus isn't allowed in C++." on page 323. While i...
6
by: Michael.Suarez | last post by:
Consider the TextBox Control. It has a KeyPress event of type KeyPressEventHandler which passes a KeyPressEventArgs to whatever method is assigned to the event. When you set e.Handled = false...
46
by: Steven T. Hatton | last post by:
I just read §2.11.3 of D&E, and I have to say, I'm quite puzzled by what it says. http://java.sun.com/docs/books/tutorial/essential/concurrency/syncrgb.html <shrug> -- NOUN:1. Money or...
13
by: markn | last post by:
Running some code through static analysis, I noticed that gcc will generate a warning if a function returns an aggregate, controlled with this flag (from the gcc manual): -Waggregate-return...
127
by: sanjay.vasudevan | last post by:
Why are the following declarations invalid in C? int f(); int f(); It would be great if anyone could also explain the design decision for such a language restricton. Regards, Sanjay
12
by: Matt B | last post by:
I was just wondering if there is a "best" choice from the following couple of ways of returning a value from a method: 1) private HashAlgorithm GetSpecificHashAlgorithm(string hashString){ if...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.