Home > Archive > VC Language > November 2005 > function with pointer
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
function with pointer
|
|
|
| Please help!
I have a class Book with private data char * ptrTitle which pointing to an
memory storing the book title. I set the Title using the following function
void Book::setTitle(string inTitle)
{
ptrBookTitle = new char[strlen(inTitle.c_str())+1];
strcpy(ptrBookTitle, inTitle.c_str());
}
I don't know how to write the function that returning the book title. Please
help!
| |
| Scott McPhillips [MVP] 2005-11-25, 3:59 am |
| alee wrote:
> Please help!
>
> I have a class Book with private data char * ptrTitle which pointing to an
> memory storing the book title. I set the Title using the following function
> void Book::setTitle(string inTitle)
> {
> ptrBookTitle = new char[strlen(inTitle.c_str())+1];
> strcpy(ptrBookTitle, inTitle.c_str());
> }
>
> I don't know how to write the function that returning the book title. Please
> help!
That's because you lost the pointer ptrBookTitle. Save it in a member
variable so you can use it in other member functions.
--
Scott McPhillips [VC++ MVP]
| |
| John Carson 2005-11-25, 3:59 am |
| "alee" <alee@discussions.microsoft.com> wrote in message
news:4B68C980-8C4F-47FF-A5C7-8EACDF3A4F5E@microsoft.com
> Please help!
>
> I have a class Book with private data char * ptrTitle which pointing
> to an memory storing the book title. I set the Title using the
> following function
> void Book::setTitle(string inTitle)
> {
> ptrBookTitle = new char[strlen(inTitle.c_str())+1];
> strcpy(ptrBookTitle, inTitle.c_str());
> }
I am a little . Your private data is a pointer called ptrTitle and
you assign to ptrBookTitle. Is your private data really ptrBookTitle. The
variable name should be the same in both cases.
Incidentally, you don't need to calculate the length of inTitle. string
class objects store their length and you can retrieve it with the size() or
length() member functions (the two functions are equivalent for the string
class).
> I don't know how to write the function that returning the book title.
> Please help!
Presumably you don't want the caller to be able to modify the title (callers
do that through the set function), so (assuming the member name is
ptrBookTitle) this works:
const char * Book::getTitle() const
{
return ptrBookTitle;
}
--
John Carson
| |
|
| John, Thanks very much.
Yes, you're right. I made a mistake. The private data is char * ptrBookTitle
instead.
Do you mean I can put down the body of setTitle like the followings?
ptrBookTitle = new char[];
strcpy(ptrBookTitle, inTitle.c_str());
Secondly, please help me on this one.
I have created a copy constructor for this class
CBook::CBook(const CBook& bookObject)
{
ptrBookTitle = new char[strlen(bookObject.ptrBookTitle)];
assert(ptrBookTitle);
strcpy(ptrBookTitle, bookObject.ptrBookTitle);
mNumAuthors= bookObject.mNumAuthors;
ptrAuthors = new string[mNumAuthors];
assert(ptrAuthors);
for (int i = 0; i < mNumAuthors; i++)
ptrAuthors[i] = bookObject.ptrAuthors[i];
mBookPublisher = bookObject.mBookPublisher;
mPublicationYear = bookObject.mPublicationYear;
mPrice = bookObject.mPrice;
mQuantityInStock = bookObject.mQuantityInStock;
}
on the driver program, I have declare CBook book2(book1); to declare book2
and initialize it using book1. and print the information out. But after the
program finishes the last line of code, an error message shows up. Here is
the message:
Debug Error!
Program: .... \debug\test.exe
Damage: after Normal block(#146) at 0x0...
I have tried to comment out the CBook book2(book1); and the print(); the
error message didn't show up. What is wrong with my code?
Thanks.
"John Carson" wrote:
> "alee" <alee@discussions.microsoft.com> wrote in message
> news:4B68C980-8C4F-47FF-A5C7-8EACDF3A4F5E@microsoft.com
>
> I am a little . Your private data is a pointer called ptrTitle and
> you assign to ptrBookTitle. Is your private data really ptrBookTitle. The
> variable name should be the same in both cases.
>
> Incidentally, you don't need to calculate the length of inTitle. string
> class objects store their length and you can retrieve it with the size() or
> length() member functions (the two functions are equivalent for the string
> class).
>
>
> Presumably you don't want the caller to be able to modify the title (callers
> do that through the set function), so (assuming the member name is
> ptrBookTitle) this works:
>
> const char * Book::getTitle() const
> {
> return ptrBookTitle;
> }
>
> --
> John Carson
>
>
| |
| John Carson 2005-11-25, 3:59 am |
| "John Carson" <jcarson_n_o_sp_am_@netspace.net.au> wrote in message
news:uxRA$eY8FHA.736@TK2MSFTNGP09.phx.gbl
>
> Presumably you don't want the caller to be able to modify the title
> (callers do that through the set function), so (assuming the member
> name is ptrBookTitle) this works:
>
> const char * Book::getTitle() const
> {
> return ptrBookTitle;
> }
If you want to return a string, you can use:
string Book::getTitle() const
{
return string(ptrBookTitle);
}
Incidentally, if you want to be able to set a book title more than once,
then you should include
delete ptrBookTitle;
at the beginning of your setTitle function. You also need to set
ptrBookTitle equal to zero in your constructor for this to work.
Finally, unless you have a really good reason for using a char pointer,
consider storing the book title as a string instead.
--
John Carson
| |
| John Carson 2005-11-25, 3:59 am |
| "John Carson" <jcarson_n_o_sp_am_@netspace.net.au> wrote in message
news:%23vMcHxY8FHA.1148@tk2msftngp13.phx.gbl
>
> Incidentally, if you want to be able to set a book title more than
> once, then you should include
>
> delete ptrBookTitle;
>
> at the beginning of your setTitle function.
Make that
delete[] ptrBookTitle;
--
John Carson
| |
| John Carson 2005-11-25, 3:59 am |
| "alee" <alee@discussions.microsoft.com> wrote in message
news:E986D77B-A385-4C6E-9E1A-9F6C3E855F29@microsoft.com
> John, Thanks very much.
>
> Yes, you're right. I made a mistake. The private data is char *
> ptrBookTitle instead.
>
> Do you mean I can put down the body of setTitle like the followings?
> ptrBookTitle = new char[];
No. It would be:
ptrBookTitle = new char[inTitle.length() + 1];
> strcpy(ptrBookTitle, inTitle.c_str());
> Secondly, please help me on this one.
>
> I have created a copy constructor for this class
>
> CBook::CBook(const CBook& bookObject)
> {
> ptrBookTitle = new char[strlen(bookObject.ptrBookTitle)];
You have omitted the + 1 to allow for the terminating '\0'.
If ptrBookTitle may already point to memory storing a book title, then you
will need to call delete[] before allocating new memory for ptrBookTitle to
point to, or else you will have a memory leak.
> assert(ptrBookTitle);
If using a standard compliant compiler, a failure of new will result in the
throwing of an exception, not the return of a null pointer. Moreover,
asserts only work in debug mode. Accordingly, there is no point to the
assert.
> strcpy(ptrBookTitle, bookObject.ptrBookTitle);
> mNumAuthors= bookObject.mNumAuthors;
> ptrAuthors = new string[mNumAuthors];
Once again, you need to be wary of a memory leak if a Book might already
have memory allocated for this purpose.
> assert(ptrAuthors);
See above for the use of assert to check the result of new.
> for (int i = 0; i < mNumAuthors; i++)
> ptrAuthors[i] = bookObject.ptrAuthors[i];
You are using a very strange combination of char * pointers and strings. I
strongly suggest you use strings exclusively. It will give you simpler and
less error prone code. A further step toward modern C++ style would be to
use a vector of strings rather than an array of strings.
> mBookPublisher = bookObject.mBookPublisher;
> mPublicationYear = bookObject.mPublicationYear;
> mPrice = bookObject.mPrice;
> mQuantityInStock = bookObject.mQuantityInStock;
>
> }
>
> on the driver program, I have declare CBook book2(book1); to declare
> book2 and initialize it using book1. and print the information out.
> But after the program finishes the last line of code, an error
> message shows up. Here is the message:
>
> Debug Error!
> Program: .... \debug\test.exe
> Damage: after Normal block(#146) at 0x0...
>
> I have tried to comment out the CBook book2(book1); and the print();
> the error message didn't show up. What is wrong with my code?
>
It is probably the missing + 1 referred to above that is causing this.
--
John Carson
| |
|
| John,
Thanks very much for your advice.
I am still struggling with the debug error message. Still have no clue why
it shows up after the program ends.
"John Carson" wrote:
> "John Carson" <jcarson_n_o_sp_am_@netspace.net.au> wrote in message
> news:uxRA$eY8FHA.736@TK2MSFTNGP09.phx.gbl
>
> If you want to return a string, you can use:
>
> string Book::getTitle() const
> {
> return string(ptrBookTitle);
> }
>
> Incidentally, if you want to be able to set a book title more than once,
> then you should include
>
> delete ptrBookTitle;
>
> at the beginning of your setTitle function. You also need to set
> ptrBookTitle equal to zero in your constructor for this to work.
>
> Finally, unless you have a really good reason for using a char pointer,
> consider storing the book title as a string instead.
>
>
> --
> John Carson
>
| |
|
| John,
Yes, you're right. It's the +1 that I miss causing the debug error message.
Thanks so much for help me.
"John Carson" wrote:
> "John Carson" <jcarson_n_o_sp_am_@netspace.net.au> wrote in message
> news:%23vMcHxY8FHA.1148@tk2msftngp13.phx.gbl
>
> Make that
>
> delete[] ptrBookTitle;
>
>
> --
> John Carson
>
| |
| Ulrich Eckhardt 2005-11-25, 3:59 am |
| alee wrote:
> I have a class Book with private data char * ptrTitle which pointing to an
> memory storing the book title. I set the Title using the following
> function void Book::setTitle(string inTitle)
> {
> ptrBookTitle = new char[strlen(inTitle.c_str())+1];
> strcpy(ptrBookTitle, inTitle.c_str());
> }
>
> I don't know how to write the function that returning the book title.
Please forget about the existence of pointers, new[] and functions like
strcpy and strlen until you know what they mean and what you are doing.
Until then:
class Book
{
std::string m_title;
public:
void setTitle( std::string const& t)
{ m_title = t; }
std::string const& getTitle() const
{ return m_title; }
};
Note: when you provide access to this book's title like the above, you could
as well make the title public. If it really is just a collection of data (a
so-called record) without real behaviour then you can as well use a struct.
Only when using pointers like you did it would then make sense to restrict
access to them, but std::string can take care of its internals on its own.
Uli
|
|
|
|
|