#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
class newclass
{
public:
string *Date;
void show(){ cout << this->Date;}
};
int main()
{
newclass *n = new newclass;
n->Date = new string ;
n->Date.assign("hello"); // get an error on this line
n->show();
return 0;
}
有人可以解释一下这是如何运作的吗?我是新手。
答案 0 :(得分:7)
n->Date->assign("hello")
因为n->Date
是指针,所以在调用函数之前必须取消引用它。
请查看this SO Question以获取语法的详细说明。
答案 1 :(得分:2)
你在这里遇到了一些问题,让我们一次过一个:
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
class newclass
{
public:
// This is a pointer to a std::string, that doesn't appear to
// be allocated. From this small example, you don't really need
// a pointer at all:
// string *Date;
// now that this object isn't a pointer, you can use dot syntax
// to access its member functions
string Date;
void show()
{
// accessing date through this-> is not necessary
// here. You can simply use Date. However since this
// doesn't cause any specific problems I mention it only
// as informational
cout << this->Date;
}
};
int main()
{
newclass *n = new newclass;
// This is bad practice, you generally shouldn't be allocating
// objects within a class outside of that class's implementation.
// this would better be done in the newclass constructor.
// n->Date = new string ;
// Since we replaced the pointer to string object Date in newclass
// and replaced it with an automatic string object, this line will
// now work as written.
n->Date.assign("hello"); // get an error on this line
n->show();
// At this point your program will exit, and newclass is shown as
// a memory leak. While the OS will reclaim this memory at application
// exit, its good to get used to managing the lifetime of your objects
// appropriate. This has do be deleted, or better yet wrapped in a smart
// pointer
delete n;
return 0;
}
为了提供与原始问题类似的示例,我们采用以下代码:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <memory>
using namespace std;
class newclass
{
public:
// auto_ptr is a smart pointer class that
// wraps a dynamically allocated object and
// provides cleanup for it when it goes out
// of scope. So when our class goes out of
// scope the Date object will be cleaned up
// for us
auto_ptr<string> Date;
// this is the constructor of the newclass object
// its called anytime a newclass object is instantiated
// we use the initializer list to allocate the Date object
newclass() : Date(new string)
{
}
// a mutator for setting the Date object (promotes encapsulation)
void set_date(const std::string& val)
{
// necessary to dereference to get at the date object
(*Date) = val;
}
void show() { cout << this->Date; }
};
int main()
{
// note the use of an auto_ptr here for automatic cleanup
auto_ptr<newclass> n(new newclass);
// use our mutator method to set the date
n->set_date("hello");
n->show();
return 0;
}
答案 2 :(得分:2)
看起来你正在应用其他语言的习语。在C ++中,您应尽可能避免使用指针,替换为成员或局部变量。在这种情况下,您的代码将如下所示:
class newclass
{
public:
string Date;
void show(){ cout << this->Date;}
};
int main() {
newclass n;
n.Date.assign("hello");
n.show();
return 0;
}
答案 3 :(得分:1)
Date本身就是一个指向string的指针,你也需要取消引用它:
正&GT;日期 - &GT;分配( “你好”);
答案 4 :(得分:0)
newclass *n = new newclass;
n->Date = new string ;
n->Date->assign("hello"); // use -> when accessing a member of a pointer
n->show();