473,398 Members | 2,120 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,398 software developers and data experts.

About the return type of the function !

Aff
Brothers ,
i am facing a problem which is as follow:

class poly
{
private:
char name[20];
int capacity;
public:
poly(char [], int );
char* gettername();

};

poly::poly(char a[],int cap)
{
int counter=0;
counter=strlen(a);
for(int i=0;i<counter;i++)
{
name[i]=a[i];
}

}
char* poly::gettername()
{
// here i want to return the name which is stored in the char
array(private data) .which is best way ?to do so ..and explain the
reason of doing such thing .

}

int main()
{

return 0;
}

Oct 14 '06 #1
12 1963
Aff@n wrote:
Brothers ,
i am facing a problem which is as follow:

class poly
{
private:
char name[20];
Why are you using fixed length char buffer instead of std::string?
int capacity;
public:
poly(char [], int );
char* gettername();

};

poly::poly(char a[],int cap)
{
int counter=0;
counter=strlen(a);
strlen() returns a size_t, it would be bettert to make counter that
type.
for(int i=0;i<counter;i++)
{
name[i]=a[i];
}
This fails to copy the null terminator. That's probably not what you
want if your intent is to have C-style strings.
}
char* poly::gettername()
{
// here i want to return the name which is stored in the char
array(private data) .which is best way ?
There is no best way. What's the point of having it private it you
provide an accessor?

Without knowing what your overall design goals are, it's impossible to
answer the question.
A tentative reply is dump the buffer, use std::string, either return a
copy as needed or move the code that needs the access to the member
into the class.


Brian
Oct 14 '06 #2

Aff@n wrote:
Brothers ,
i am facing a problem which is as follow:

class poly
{
private:
char name[20];
int capacity;
public:
poly(char [], int );
char* gettername();

};

poly::poly(char a[],int cap)
{
int counter=0;
counter=strlen(a);
for(int i=0;i<counter;i++)
{
name[i]=a[i];
}

}
char* poly::gettername()
{
// here i want to return the name which is stored in the char
array(private data) .which is best way ?to do so ..and explain the
reason of doing such thing .

}

int main()
{

return 0;
}
todo the best 1st make an other object which will not break
encapsulation,
char* poly::gettername()
{
char *temp;
temp = new char [20];
strcpy(temp,name);
return temp;// this will not destroy the memory untill destroyed so u
have to be carefull usning dat.
}

Oct 14 '06 #3
"Aff@n" <af********@gmail.comwrote in message
news:11**********************@k70g2000cwa.googlegr oups.com...
Brothers ,
i am facing a problem which is as follow:

class poly
{
private:
char name[20];
int capacity;
public:
poly(char [], int );
char* gettername();

};

poly::poly(char a[],int cap)
{
int counter=0;
counter=strlen(a);
for(int i=0;i<counter;i++)
{
name[i]=a[i];
}

}
char* poly::gettername()
{
// here i want to return the name which is stored in the char
array(private data) .which is best way ?to do so ..and explain the
reason of doing such thing .

}

int main()
{

return 0;
}
This is one of the problems for which std::string was designed. Just make
name a std::string and return it and your problem goes away.

class poly
(
private:
std::string name;
int capacity;
public:
poly( std::string&, int );
std::string gettername();
};

poly::poly( std::string a, int cap )
{
name = a;
}

std::string poly::gettername ()
{
return name;
}

Is there any reason you are not using std::string? And you can even use
c-style strings to initialize it. I.E.

poly MyInstance( "This Name", 10 );
Oct 14 '06 #4
>
todo the best 1st make an other object which will not break
encapsulation,
char* poly::gettername()
{
char *temp;
temp = new char [20];
strcpy(temp,name);
return temp;// this will not destroy the memory untill destroyed so u
have to be carefull usning dat.
}
No! Don't do this. It is (1) dangerous and (2) inconvenient and (3)
inefficient.

Dangerous because the user have to manually delete the pointer.

Inconvenient because the call cannot be inside an expression:

ploy obj("Hello", 5);
cout << obj.gettername(); // Oops, you have a memory leak!

Inefficient because you have to allocate and deallocate memory, you have
to do copying and counting etc.

So instead, do what many have suggested--use std::string and return a
const reference, or simply return a const pointer:

class poly
{
char name[20];

public:
const char* gettername() const
{
return name;
}

// ...
};

Oct 14 '06 #5

Aff@n wrote:
Brothers ,
i am facing a problem which is as follow:
The class name chosen here is inadequate, it would have been much
simpler to explain this issue if you had used something more relevent.
class poly
{
private:
char name[20];
int capacity;
public:
poly(char [], int );
char* gettername();

};

poly::poly(char a[],int cap)
{
int counter=0;
counter=strlen(a);
for(int i=0;i<counter;i++)
{
name[i]=a[i];
}

}
char* poly::gettername()
{
// here i want to return the name which is stored in the char
array(private data) .which is best way ?to do so ..and explain the
reason of doing such thing .

}

int main()
{

return 0;
}
Doing the above with a std::string is so much easier because no
pointers of any kind are needed as far as the programmer is concerned.
Pointers == bugs and arrays are evil (specially char arrays).

What pointers do in the case of a char* is HIDE the fact thats its not
a pointer to a single character but a pointer to an array of characters
that is being accessed. Also, you have no choice but accept the fact
that a space may be inserted in that char array (something a
std::string takes with no problem). hence, you must provide the length
of the string to be stored in your ctor (yes...more bad news).

In order to properly store a character array, you need to dynamically
allocate the incoming array using a member pointer. And you need to
protect that pointer by declaring get() as a const member function or
you will be dealing with nasty side effects (where some idiot uses the
get() function to reset the internal pointer). Not good, very nasty.

I'm not done yet. Copying an instance of a class like poly requires
special considerations. You have no choice but to hard-code a copy ctor
in order to allocate a new array and hence set the critical member
pointer. A std::string member would have copied automatically because a
std::string already has its own copy ctor, unlike a char[] which
doesn't know what a copy ctor is.
Wait, we haven't discussed assignment yet... lets disable it for now.

Are you getting the drift? Pointers and Arrays means much more work and
a lot more code, not to mention special considrations.
Incidentally, in the code below: the type unsigned (integer) should be
replaced with size_t, we'll ignore that for now. Also, you have to
verify that your compiler inserts the null terminator when:
const char a[] = "a short string with spaces"; // verify the str length
output below

#include <iostream>

class poly
{
char* p_name;
public:
poly( const char*, unsigned length );
poly(const poly& copy);
~poly();
poly& operator=(const poly& rhv); // disabled
const char* getStr() const;
};

poly::poly( const char* p_a, unsigned length )
{
std::cout << "length = " << length;
std::cout << std::endl;
p_name = new char[length]; // heap allocation
for(unsigned i = 0; i < length; ++i)
{
p_name[i] = *p_a++;
}
}

poly::poly(const poly& copy)
{
unsigned sz = *copy.p_name;
p_name = new char[sizeof(sz)];
for(unsigned i = 0; i < sz; ++i)
{
p_name[i] = copy.p_name[i];
}
}

poly::~poly()
{
delete [] p_name; // deallocate array
}

const char* poly::getStr() const
{
return p_name;
}

int main()
{
const char a[] = "a short string with spaces";
poly test(a, sizeof(a)); // length is required
std::cout << test.getStr() << std::endl;

poly duplicate = test; // copy test
std::cout << duplicate.getStr() << std::endl;

return 0;
}

/*
length = 27
a short string with spaces <- 26 characters + '/0'
a short string with spaces
*/

Oct 14 '06 #6

Jim Langston wrote:
"Aff@n" <af********@gmail.comwrote in message
news:11**********************@k70g2000cwa.googlegr oups.com...
Brothers ,
i am facing a problem which is as follow:

class poly
{
private:
char name[20];
int capacity;
public:
poly(char [], int );
char* gettername();

};

poly::poly(char a[],int cap)
{
int counter=0;
counter=strlen(a);
for(int i=0;i<counter;i++)
{
name[i]=a[i];
}

}
char* poly::gettername()
{
// here i want to return the name which is stored in the char
array(private data) .which is best way ?to do so ..and explain the
reason of doing such thing .

}

int main()
{

return 0;
}

This is one of the problems for which std::string was designed. Just make
name a std::string and return it and your problem goes away.

class poly
(
private:
std::string name;
int capacity;
public:
poly( std::string&, int );
std::string gettername();
};

poly::poly( std::string a, int cap )
{
name = a;
}

std::string poly::gettername ()
{
return name;
}

Is there any reason you are not using std::string? And you can even use
c-style strings to initialize it. I.E.

poly MyInstance( "This Name", 10 );
I'll second the above.
Allow me to clarify the code or the OP is gonna freak:

class poly
(
std::string name;
public:
poly( std::string );
std::string gettername() const;
};

poly::poly( std::string a ) : name( a )
{
}

std::string poly::gettername() const
{
return name;
}

int main()
{
poly MyInstance("This Name");
std::cout << MyInstance.gettername() << std::endl;
}

Oct 14 '06 #7

Salt_Peter wrote:
Aff@n wrote:
Brothers ,
i am facing a problem which is as follow:

The class name chosen here is inadequate, it would have been much
simpler to explain this issue if you had used something more relevent.
class poly
{
private:
char name[20];
int capacity;
public:
poly(char [], int );
char* gettername();

};

poly::poly(char a[],int cap)
{
int counter=0;
counter=strlen(a);
for(int i=0;i<counter;i++)
{
name[i]=a[i];
}

}
char* poly::gettername()
{
// here i want to return the name which is stored in the char
array(private data) .which is best way ?to do so ..and explain the
reason of doing such thing .

}

int main()
{

return 0;
}

Doing the above with a std::string is so much easier because no
pointers of any kind are needed as far as the programmer is concerned.
Pointers == bugs and arrays are evil (specially char arrays).

What pointers do in the case of a char* is HIDE the fact thats its not
a pointer to a single character but a pointer to an array of characters
that is being accessed. Also, you have no choice but accept the fact
that a space may be inserted in that char array (something a
std::string takes with no problem). hence, you must provide the length
of the string to be stored in your ctor (yes...more bad news).

In order to properly store a character array, you need to dynamically
allocate the incoming array using a member pointer. And you need to
protect that pointer by declaring get() as a const member function or
you will be dealing with nasty side effects (where some idiot uses the
get() function to reset the internal pointer). Not good, very nasty.

I'm not done yet. Copying an instance of a class like poly requires
special considerations. You have no choice but to hard-code a copy ctor
in order to allocate a new array and hence set the critical member
pointer. A std::string member would have copied automatically because a
std::string already has its own copy ctor, unlike a char[] which
doesn't know what a copy ctor is.
Wait, we haven't discussed assignment yet... lets disable it for now.

Are you getting the drift? Pointers and Arrays means much more work and
a lot more code, not to mention special considrations.
Incidentally, in the code below: the type unsigned (integer) should be
replaced with size_t, we'll ignore that for now. Also, you have to
verify that your compiler inserts the null terminator when:
const char a[] = "a short string with spaces"; // verify the str length
output below

#include <iostream>

class poly
{
char* p_name;
public:
poly( const char*, unsigned length );
poly(const poly& copy);
~poly();
poly& operator=(const poly& rhv); // disabled
const char* getStr() const;
};

poly::poly( const char* p_a, unsigned length )
{
std::cout << "length = " << length;
std::cout << std::endl;
p_name = new char[length]; // heap allocation
for(unsigned i = 0; i < length; ++i)
{
p_name[i] = *p_a++;
}
}

poly::poly(const poly& copy)
{
unsigned sz = *copy.p_name;
p_name = new char[sizeof(sz)];
for(unsigned i = 0; i < sz; ++i)
{
p_name[i] = copy.p_name[i];
}
}

poly::~poly()
{
delete [] p_name; // deallocate array
}

const char* poly::getStr() const
{
return p_name;
}

int main()
{
const char a[] = "a short string with spaces";
poly test(a, sizeof(a)); // length is required
std::cout << test.getStr() << std::endl;

poly duplicate = test; // copy test
std::cout << duplicate.getStr() << std::endl;

return 0;
}

/*
length = 27
a short string with spaces <- 26 characters + '/0'
a short string with spaces
*/
And for the record, compare that with a std::string:

class poly
(
std::string name;
public:
poly( std::string );
std::string gettername() const;
};

poly::poly( std::string s ) : name(s)
{
}

std::string poly::gettername() const
{
return name;
}

int main()
{
poly test("a short string with spaces");
std::cout << test.gettername() << std::endl;
poly duplicate = test;
std::cout << duplicate.gettername() << std::endl;
}

Oct 14 '06 #8
And for the record, compare that with a std::string:
>
class poly
(
std::string name;
public:
poly( std::string );
std::string gettername() const;
};

poly::poly( std::string s ) : name(s)
{
}

std::string poly::gettername() const
{
return name;
}
That is fine, but why not returning a const std::string& ?

Ben
Oct 14 '06 #9

"benben" <benhonghatgmaildotcom@nospamwrote in message
news:45***********************@news.optusnet.com.a u...
>poly::poly( std::string s ) : name(s)
{
}

std::string poly::gettername() const
{
return name;
}

That is fine, but why not returning a const std::string& ?
Or having the ctor take const std::string& ?
Oct 14 '06 #10
benben wrote:
And for the record, compare that with a std::string:

class poly
(
that should have been

class poly
{
std::string name;
public:
poly( std::string );
std::string gettername() const;
};

poly::poly( std::string s ) : name(s)
{
}

std::string poly::gettername() const
{
return name;
}

That is fine, but why not returning a const std::string& ?

Ben
of course, why not?
http://www.parashift.com/c++-faq-lit...html#faq-18.11
Note that the result will be the same in either case - a single copy
ctor gets invoked.

const std::string& poly::gettername() const
{
return name;
}

Oct 14 '06 #11
Salt_Peter schrieb:
benben wrote:
>>std::string poly::gettername() const
{
return name;
}
That is fine, but why not returning a const std::string& ?

Ben

of course, why not?
http://www.parashift.com/c++-faq-lit...html#faq-18.11
Note that the result will be the same in either case - a single copy
ctor gets invoked.

const std::string& poly::gettername() const
{
return name;
}
Why should a copy ctor gets called when returning a reference?

poly p;
std::cout << p.gettername();

When returning by value, a temporary is copy-ctored. When returning by
reference, only the reference is returned.

--
Thomas
http://www.netmeister.org/news/learn2quote.html
Oct 14 '06 #12

Thomas J. Gritzan wrote:
Salt_Peter schrieb:
benben wrote:
>std::string poly::gettername() const
{
return name;
}
That is fine, but why not returning a const std::string& ?

Ben
of course, why not?
http://www.parashift.com/c++-faq-lit...html#faq-18.11
Note that the result will be the same in either case - a single copy
ctor gets invoked.

const std::string& poly::gettername() const
{
return name;
}

Why should a copy ctor gets called when returning a reference?

poly p;
std::cout << p.gettername();
Good point.
>
When returning by value, a temporary is copy-ctored. When returning by
reference, only the reference is returned.

--
Thomas
http://www.netmeister.org/news/learn2quote.html
Oct 14 '06 #13

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

Similar topics

2
by: lawrence | last post by:
I've been bad about documentation so far but I'm going to try to be better. I've mostly worked alone so I'm the only one, so far, who's suffered from my bad habits. But I'd like other programmers...
35
by: wired | last post by:
Hi, I've just taught myself C++, so I haven't learnt much about style or the like from any single source, and I'm quite styleless as a result. But at the same time, I really want nice code and I...
3
by: Minh Khoa | last post by:
Please give me more information about delegate and its usage? Why do i use it and when?
7
by: Adrian Parker | last post by:
'function to convert null to nothing Function CheckDate(ByVal DRow As DataRow, ByVal strCol As String) As Date If DRow.Item(strCol) Is System.DBNull.Value Then Return Nothing Else Return...
105
by: Christoph Zwerschke | last post by:
Sometimes I find myself stumbling over Python issues which have to do with what I perceive as a lack of orthogonality. For instance, I just wanted to use the index() method on a tuple which does...
7
by: Yuri_Юрий | last post by:
I'm confused about the VARIABLE LENGTH ARRAYS. {scanf("%d",&n);float a;} In which compiler can I use it? I tried VC++6.0 SP6,but it's reported error:CONSTANT EXPRESSION! Another question, What...
68
by: James Dow Allen | last post by:
The gcc compiler treats malloc() specially! I have no particular question, but it might be fun to hear from anyone who knows about gcc's special behavior. Some may find this post interesting;...
1
by: UJ | last post by:
I am doing development on a machine and everything was working fine. The name of the project was ECS to I made all my references as ~/ECS/... Worked great. Put it on the final server running...
35
by: rebeccatre | last post by:
hi can Variant archiving setTimout('.. capability be done without using it? :-)
10
by: colin | last post by:
Hi, I profile my code and find its spending a lot of time doing implicit conversions from similar structures. the conversions are mainly things like this class Point { implicit conversion...
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: 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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.