|
You asked the same question - please don't keep reposting the same question.
You're probably not getting an answer as you don't really give much info. Today you're getting an error. What is the error? Have you tried stepping through in a debugger when you click your list control? What happens?
|
|
|
|
|
|
Hi, i'm learning c++ and i'm having a problem using a class.
My class is defined like this:
// ****************************************************
class CTest
{
public:
CString *sName;
CTest();
~CTest();
};
CTest::CTest()
{
this->sName=NULL;
}
CTest::~CTest()
{
if (this->sName!=NULL)
delete[] (this->sName);
}
// ****************************************************
And used like this:
CTest test;
test.sName = new CString("Just a test");
It works, but when test is destructed it gives me an unhandled exception error. I though that when you used a pointer to something like an array, you had to use delete[]. I've tried without the [] and it works, but i think there will be a memory leak, no?
Hope someone can explain this to me.
Thanks in advance,
Rad.
|
|
|
|
|
new -> use delete
new[] -> use delete[]
|
|
|
|
|
Thanks for the quick reponse! I was worried about the memory leaks . Thank you.
|
|
|
|
|
Just a question: why are you using a pointer ? You know that CString is a class and you don't need to work with it the same way as a standard char array ?
You can simply have declared it CString sName and use sName. The CString class will manage everything for you.
Also, on a side note:
this->sName=NULL;
I never understood why people were using the this pointer everytime they access a member variable
|
|
|
|
|
Well, you were right
I don't need to use a pointer. I'm still thinking in a "C way", and, as I didn't want to use more memory than necessary I thought that the best way was to use pointers. After your question I've tried and it looks like the CString class implements all that I need.
And, about the other question... hey, I'm learning!
Thanks for the suggestions,
Rad.
|
|
|
|
|
The C++ way is: always use objects or references unless you need to use pointers. If you must use pointers then prefer smart pointers. Basically, make life as easy for yourself as possible. Only make it difficult when you have to.
Kevin
|
|
|
|
|
Cedric Moonen wrote: I never understood why people were using the this pointer everytime they access a member variable
matter of readability of the code... i use it also but muchfor member functions (for data members, the leading m_ already tells it's a member of the class)
|
|
|
|
|
I don't find that very readable in fact
For member variables, I use the leading m_ as you do. But for member functions, in fact, as I almost never have global functions,I don't need the this .
|
|
|
|
|
i don't use Globals either, but that's a matter of taste i believe... you don't like it, i do... maybe a poll could tell us which rate of the population here uses this syntax
|
|
|
|
|
Cedric Moonen wrote: in fact, as I almost never have global functions,
By "global functions", do you mean you never have functions outside of classes, or that all your standalone functions are in their own namespace?
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Zac Howland wrote: By "global functions", do you mean you never have functions outside of classes, or that all your standalone functions are in their own namespace?
I mean I almost never have functions outside of classes. Of course, I still call global functions from the API (I didn't think of that when I posted my message ) but in general you I recognize them easily.
|
|
|
|
|
Ah, I see. Now my question would be, why the hell not!? You force yourself into the limitations of OOD by choice? There are actually some functions you can't write as member functions (at least not and have them functional).
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Zac Howland wrote: You force yourself into the limitations of OOD by choice?
Not at all. In fact, I have in general some functions that are global but they are limited (and then, I use namespace if it is possible to group them). In fact, I never came to a situation like: "ok, I have this function and I don't know in which class to put it, let's make a class to wrap it".
|
|
|
|
|
Cedric Moonen wrote: Not at all. In fact, I have in general some functions that are global but they are limited (and then, I use namespace if it is possible to group them). In fact, I never came to a situation like: "ok, I have this function and I don't know in which class to put it, let's make a class to wrap it".
Ah, so you do use globals behind a namespace then? You had me worried there for a second
Generally, when I'm writing classes, I try to not put as many functions as possible (that make sense, obviously) inside the class. They will be "packaged" with the class (usually defined in the same header file and part of the same namespace), but not actually be in the class.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Cedric Moonen wrote: I never understood why people were using the this pointer everytime they access a member variable
Honestly, I find that most people use it because typing "this->" will trigger Intellisense so they don't have to type out the whole member name they are trying to access (e.g. laziness).
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Zac Howland wrote: I find that most people use it because typing "this->" will trigger Intellisense
And typing all that and scrolling through list of possible auto completes is more efficient?
|
|
|
|
|
Mark Salsbery wrote: ...and scrolling through list of possible auto completes...
If the list even shows up.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
DavidCrow wrote: If the list even shows up.
He he
Who am I to talk....I still use Hungarian Notation (dwVariable, nVariable, etc.) so I'm sure what
variable types are at a glance. Lots of people laugh at that but Microsoft uses it in their
code and Microsoft is our master therefore we must obey.
Mark
|
|
|
|
|
Mark Salsbery wrote: I still use Hungarian Notation
Be careful, you might start a religious war!
Kevin
|
|
|
|
|
Hey wasn't there "Polish" notation at one time. Something to do with calculators and algabraic
notation...
|
|
|
|
|
Mark Salsbery wrote: Who am I to talk....I still use Hungarian Notation (dwVariable, nVariable, etc.) so I'm sure what
variable types are at a glance.
Yeah, I still do that in my code as well ... and even in the Java code I was writing not too long ago .
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Zac Howland wrote: Yeah, I still do that in my code as well ... and even in the Java code I was writing not too long ago
I tried briefly at one time to stop using it and be "cool" but it drove me nuts having to
look for types when I'd rather just have a good idea (KNOW) just by looking at it.
Thankfully, I remember NO JAVA. Last time I used it it was version 1.x and compiler was command-
line. If I ever have to use it again I'll fall on my sword
Cheers,
Mark
|
|
|
|
|
Mark Salsbery wrote: And typing all that and scrolling through list of possible auto completes is more efficient?
To me, no. I type faster than the IDE triggers Intellisense anyway. But I've seen coworkers do it for just that reason (and "claim" higher efficiency).
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|