printf("%s",myclass)打印vtable而不是m_data [英] printf("%s", myclass) prints vtable instead of m_data
问题描述
class MyString
{
char * m_data;
public:
MyString(const char * c)
{
m_data = new char [1024];
strcpy( m_data,c);
}
virtual~MyString()
{
delete [] m_data; < br $>
}
};
int main()
{
MyString ms(KungFoo);
print(" Here is:%s",ms);
}
this打印vtable指针的地址(VC ++ 7.1),而不是
m_data。有什么方法可以让它工作,所以& ms指向
到ms.m_data?
我知道 - 删除虚拟关键字会做,但是任何其他方式?
-
-Gernot
int main(int argc,char ** argv){printf
(%silto%c%cf%cgl%ssic%ccom%c,ma,58,g,64,ba,46,10);}
Hi,
class MyString
{
char* m_data;
public:
MyString(const char* c)
{
m_data = new char[1024];
strcpy(m_data, c);
}
virtual ~MyString()
{
delete[] m_data;
}
};
int main()
{
MyString ms("KungFoo");
print("Here is: %s", ms);
}
this prints the address of the vtable pointer (VC++7.1) instead of
m_data. Is there any way of getting this to work, so that &ms points
to ms.m_data?
I know - removing the virtual keyword will do, but any other way?
--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, ''g'', 64, "ba", 46, 10);}
推荐答案
Gernot Frisch< Me@Privacy.netwrote:
Gernot Frisch <Me@Privacy.netwrote:
class MyString
{
char * m_data;
public:
MyString(const char * c)
{
m_data = new char [1024];
strcpy(m_data, c);
}
虚拟~MyString()
{
delete [] m_data;
}
};
int main()
{
MyString ms(KungFoo);
print(&q uot;这里是:%s",ms);
}
打印vtable指针的地址(VC ++ 7.1)而不是
m_data。有没有办法让这个工作,所以& ms指向
到ms.m_data?
Hi,
class MyString
{
char* m_data;
public:
MyString(const char* c)
{
m_data = new char[1024];
strcpy(m_data, c);
}
virtual ~MyString()
{
delete[] m_data;
}
};
int main()
{
MyString ms("KungFoo");
print("Here is: %s", ms);
}
this prints the address of the vtable pointer (VC++7.1) instead of
m_data. Is there any way of getting this to work, so that &ms points
to ms.m_data?
提供函数c_str(或任何你想要调用的函数),
返回char *。然后调用printf,如:
printf(" Here is:%s",ms.c_str());
你无法通过非POD通过省略号,afaik。
Provide a function c_str (or whatever you want to call it), which
returns the char*. Then call printf like:
printf ("Here is: %s", ms.c_str ());
You cannot pass non-PODs thru the ellipsis, afaik.
我知道 - 删除虚拟关键字会做什么,但是还有其他办法吗?
I know - removing the virtual keyword will do, but any other way?
删除虚拟关键字可能适用于您的情况,但它仍然无效,因为您仍在通过非POD通过省略号。
hth
-
jb
(回复地址在rot13中,首先解读)
Removing the virtual keyword may work in your case, but it is still
invalid, as you are still passing a non-POD thru an ellipsis.
hth
--
jb
(reply address in rot13, unscramble first)
Gernot Frisch schrieb:
Gernot Frisch schrieb:
class MyString
{
char * m_data;
public:
MyString(const char * c)
{
m_data = new char [1024];
strcpy(m_data,c);
Hi,
class MyString
{
char* m_data;
public:
MyString(const char* c)
{
m_data = new char[1024];
strcpy(m_data, c);
你真的不想要这个。这迟早会导致
访问冲突。如果c是nullptr会发生什么?如果c确实会发生什么?
没有指向空终止的C样式字符串?如果c是什么会发生什么?
1025个字符长?
You really don''t want this. This will result, sooner or later, in an
access violation. What happens if c is nullptr? What happens if c does
not point to a null terminated C-style-string? What happens if c is is
1025 characters long?
}
virtual~MyString()
{
删除[] m_data;
}
};
int main()
{
MyString ms(" KungFoo");
print(" Here is:%s",女士);
}
virtual ~MyString()
{
delete[] m_data;
}
};
int main()
{
MyString ms("KungFoo");
print("Here is: %s", ms);
printf
printf
}
打印地址vtable指针(VC ++ 7.1)代替
m_data。有什么方法可以让它工作,所以& ms指向
到ms.m_data?
}
this prints the address of the vtable pointer (VC++7.1) instead of
m_data. Is there any way of getting this to work, so that &ms points
to ms.m_data?
No.& ms将始终指向实例ms的地址。顺便说一下,你提供了ms,而不是& ms来打印(f)
。如果您使用
copy'''''paste而不是一直在新闻阅读器中输入源代码,您将节省自己的时间,
包括输入错误或其他。
No. &ms will always point to the address of the instance ms. BTW, you
provided ms, not &ms to print(f). You''ll save your own time if you use
copy''n''paste instead of typing source in your newsreader all the time,
including typing errors or whatever.
我知道 - 删除虚拟关键字会有什么办法吗?
I know - removing the virtual keyword will do, but any other way?
否,它不会这样做。你很幸运,你的程序没有
崩溃。
总结:
*你想要std ::字符串,或者
*你想要实现一个MyString :: c_str()方法,或者
*你想要使用< iostream> s并实现一个自定义
运算符<<对于ostream和MyString。
只是:
const char * MyString :: c_str()
{
返回m_data;
}
和:
printf(" %s",ms.c_str());
或:
cout<< ms.c_str()<< endl;
或(假设您实现了运算符<< for ostreams):
cout<< ms<< endl;
这是,顺便说一句,或多或少(省略一些细节)std :: string
的作用。你可能确实想要使用std :: string,它不是世界上最好的字符串类设计,但它比任何东西都要好得多
你和我会永远发明。
祝你好运,
- Markus
No, it will not do. You just have been lucky that your program didn''t
crash.
Summarized:
* you want std::string, or
* you want to implement a MyString::c_str() method, or
* you want to use <iostream>s and implement a custom
operator<< for ostream and MyString.
Just i.e.:
const char* MyString::c_str()
{
return m_data;
}
and:
printf("%s", ms.c_str());
or:
cout << ms.c_str() << endl;
or (given that you implemented operator<< for ostreams):
cout << ms << endl;
This is, btw, more or less (omitting some details) what std::string
does. You probably do want to use std::string, it is not the worlds
best design of a string class, but its quite much better than anything
you and me will ever invent.
Best regards,
-- Markus
>
>
Markus Grueneis写道:
Markus Grueneis wrote:
class MyString
{
char * m_data;
public:
MyString(const char * c)
{
m_data = new char [1024];
strcpy(m_data,c);
class MyString
{
char* m_data;
public:
MyString(const char* c)
{
m_data = new char[1024];
strcpy(m_data, c);
你真的不想要这个。这迟早会导致
访问冲突。如果c是nullptr会发生什么?如果c确实会发生什么?
没有指向空终止的C样式字符串?如果c是什么会发生什么?
1025个字符长?
You really don''t want this. This will result, sooner or later, in an
access violation. What happens if c is nullptr? What happens if c does
not point to a null terminated C-style-string? What happens if c is is
1025 characters long?
c为空或不指向以空字符结尾的字符串是一个问题
但是std :: string也是如此。
1024是任意的,我没有看到它的原因。
除此之外,上面的类有缺陷:
1.没有定义的复制构造函数或赋值,也没有在
a中禁用默认值无效的情况。
2.无法访问字符串。
虽然他可能会添加更多方法(毕竟为什么有一个没有其他虚拟的
虚拟析构函数方法?)
c being null or not pointing to a nul-terminated string are a problem
with std::string too though.
It is true that 1024 is arbitrary and I don''t see the reason for it.
Apart from that the class above is flawed in:
1. No defined copy-constructor or assignment, nor are they disabled in
a case where the default ones are not valid.
2. No means to access the string.
although perhaps he intended to add more methods (after all why have a
virtual destructor with no other virtual methods?)
只是:
const char * MyString :: c_str()
{
返回m_data;
}
Just i.e.:
const char* MyString::c_str()
{
return m_data;
}
你错过了一个const。或者投入太多。实际上你可以这样做:
char * MyString :: c_str()const
{
返回m_data;
}
但可能不想,因为你可能想要一个const访问
函数来返回非可写字符串(深const)。
You missed a const. Or put in one too many. Actually you can do this:
char* MyString::c_str() const
{
return m_data;
}
but probably wouldn''t want to as you''d probably want a const access
function to return a non-writable string (deep const).
这是,顺便说一句,或多或少(省略一些细节)什么是std :: string
。你可能确实想要使用std :: string,它不是世界上最好的字符串类设计,但它比任何东西都要好得多
你和我会永远发明。
This is, btw, more or less (omitting some details) what std::string
does. You probably do want to use std::string, it is not the worlds
best design of a string class, but its quite much better than anything
you and me will ever invent.
但可能不是相同的语义,因此可能有理由
来编写一个字符串类。
>
例如,使用std :: string:
1.无法获取可写缓冲区的方法。
2.无锁定方法
3.非永久性
4.图书馆之间无法安全携带。
出于任何原因,您可能希望写一个字符串类。
为了锁定你可能会用
std :: string来实现它(即你的类有一个嵌套的std ::字符串并包装它。)
那并不是说std :: string应该具备所有这些功能,只是
表示没有必须只有一个字符串类。
But possibly not the same semantics, and therefore there may be reasons
to write a string class.
For example, with std::string:
1. No method to get a writable buffer.
2. No methods for locking
3. Not immutable
4. Not safely portable between libraries.
For any number of those reasons you might wish to write a string class.
For the purpose of locking you''d possibly implement it in terms of
std::string (i.e. your class has a nested std::string and wraps it).
That''s not saying that std::string should have all those features, just
that there doesn''t have to be only one string class.
这篇关于printf("%s",myclass)打印vtable而不是m_data的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!