Re: Problems compiling simple C++ code (also problems with std::string)

Tech-Archive recommends: Fix windows errors by optimizing your registry



"Susan Baker" <sbaker@xxxxxxxxxxx> wrote in message
news:d9hve0$6ka$1@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> First of all, I am having problems with std::string class as follows:
>
> 1). I wanted to initialize it to NULL so I could check later, if it
> needs to be deleted later.

First, there ain't no such thing as a NULL string, just as there is no
NULL int. A string can be empty, but there is no special value that
could be denoted as NULL

Second, std::string manages its own memory, you don't need to "delete"
it, whatever that might mean. The class will free in its destructor any
memory it might have allocated.

> 2). I can't also check for equality, as you can see in the code
> snippet above, I don't want to do anything if name is NULL (still
> using C pointer parlance here) - How do I check that variable name
> has been initialized or is not empty?

if (name.empty()) {}

> 3). Most annoying (and inexplicable to me) so far is the fact that in
> the constructor shown above, the compiler somehow, does not recognize
> the fact that the methods are "bound" to a class, and therefore,
> forbids the use of the this keyword. Here is the actual compiler
> error message: "MyClass.cpp(33): error C2228: left of '.name' must
> have class/struct/union type" - Arrgh!!. Could someone please tell me
> what I'm doing wrong?.

'this' is a pointer. You probably mean this->name and this->type . And
while we are at it, since MyClass::name is not a pointer (I assume it is
an std::string, not an std::string*), you don't allocate an intance with
new. Just do

this->name = name;

Better still, use constructor initialization list:

MyClass::MyClass(std::string name, int type) :
name(name),
type(type)
// not sure this works - consider giving different names to
parameters
{
}

--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


.



Relevant Pages

  • Re: Problems compiling simple C++ code (also problems with std::string)
    ... I wanted to initialize it to NULL so I could check later, ... using C pointer parlance here) - How do I check that variable name ... the constructor shown above, the compiler somehow, does not recognize ... error message: "MyClass.cpp: error C2228: left of '.name' must ...
    (microsoft.public.vc.language)
  • Re: CString
    ... My customers don't give a hoot in hell who forgot to initialize a variable --- if the software comes from me, ... Empty(), or set the string explicitly, always, before assuming anything. ... IMO, if you dont need a function call, dont call it. ...
    (microsoft.public.vc.mfc)
  • Re: HIGH Decimal ?
    ... initialize a minimization or maximization algorithm. ... Consider a possibly empty list of values. ... This concept is most easily expressed using APL notation. ... {nand}/e gives a DOMAIN error since nand has no identity and the relationship stated above does not hold in general even for non empty boolean vectors since nand is not associative. ...
    (comp.lang.pl1)
  • Re: Beginners question: assigning same value to many variables
    ... Subject: Beginner's question: assigning same value to ... I presume you want each variable initialized to a _different_ empty ... string. ... it's not too hard to initialize a group of instance variables to ...
    (comp.lang.ruby)
  • Re: [crash, bisected] Re: [PATCH 3/4] x86_64: Fold pda into per cpu area
    ... To initialize the percpu data you do want to talk to the virtual address ... It might be worth saying something like. ... symbols out of empty sections and making them absolute. ...
    (Linux-Kernel)