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
*/