Re: why does this happen?
From: hk_mp5kpdw (andrew_a_hanson_at_msn.com)
Date: 03/29/05
- Next message: Billy Patton: "Re: why does this happen?"
- Previous message: Thomas Matthews: "Re: why does this happen?"
- In reply to: Billy Patton: "why does this happen?"
- Next in thread: Billy Patton: "Re: why does this happen?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 29 Mar 2005 12:07:48 -0800
> test_t is a c structure but sontaing the STL string.
> In may data I cannot use new yet because of all these structures.
> So malloc is creating the memory but it does not initialize the
string path
> because it was done by malloc instead of new.
structs are a part of C++ just as much as they are in C. It is just
that typically you see classes being talked about and used much more
than structs. Bottom line, structs DO EXIST IN C++. You should have
no problem using new even when allocating memory for a struct. A
struct can even have member functions (including constructors and
destructors typically associated with a class).
> Is there any way around this?
Use "new":
#include <string>
using namespace std;
typedef struct test
{
string path;
int val;
test(const string& p) : path(p) {}
} test_t;
typedef test_t *test_p;
test_p foo(const string& path)
{
test_p temp = new test_t(path);
if( temp ) return temp;
else return NULL;
}
int main(void)
{
test_p tp = foo("abcd");
return 0;
}
- Next message: Billy Patton: "Re: why does this happen?"
- Previous message: Thomas Matthews: "Re: why does this happen?"
- In reply to: Billy Patton: "why does this happen?"
- Next in thread: Billy Patton: "Re: why does this happen?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|