类指针本身有类成员吗?
do class pointers themselves have class members ?
我想清除一些概念
1-指针只能存储一个地址,指针本身不能像任何其他变量那样存储数据,对吧?(因为下面的代码没有运行(
int *myptr;
*myptr=20;
cout<<(*myptr);
2-如果您创建一个类的指针,例如FOO
class foo
{
public:
int numb1 ;
char alphabet;
}
// this doesn't run
void main()
{
foo *myptr ;
cout<< myptr->numb1;
}
所以我的问题是类 foo (*myptr( 的指针会有变量 numb1 和 alphabet 吗? 如果不是,那么 Foo 指针和 int 指针有什么区别(除此之外,每个指针只能指向它各自的数据类型(
指针具有足够的存储空间来包含表示内存中位置的数字。完全可以使用此空间来存储其他信息(信息仍然需要适合指针的存储(。
例如,您可以在指针中存储一个长整型值:
#include <iostream>
using namespace std;
int main() {
void *ptr;
ptr = (void*)20;
long information = reinterpret_cast<long>(ptr);
std::cout<<information<<std::endl;
return 0;
}
你可以在这里尝试一下,看看它会输出数字 20。
编辑:这里有一个非空类型的指针
#include <iostream>
using namespace std;
struct test{int a;};
int main() {
// your code goes here
test* ptr;
ptr = (test*)20;
long information = reinterpret_cast<long>(ptr);
std::cout<<information<<std::endl;
return 0;
}
相关文章:
- 从成员指针到整个结构/类的强制转换
- OOP 中的单成员指针
- 使用结构成员指针在C++中填充结构
- 聚合初始化,将成员指针设置为同一结构成员
- 空指针常量 (nullptr)、空指针值和空成员指针值之间有什么区别?
- 共享 C++ 的数据成员指针
- 如何声明指向成员内容的成员指针m_pmd/m_pmf并访问它们?
- 从类C++外部调用指向成员方法的成员指针
- 结构成员指针是否自动初始化为零?
- 如何删除类内类类型的类成员指针
- 是否打印指向已定义 int 的成员指针
- 指向常量的成员指针
- 通过此指针访问时的成员差异和自身的成员指针(简单的 QT 示例问题)
- 类数据成员指针的非类型模板参数包无法使用 gcc 编译
- 为什么我不能在同一行中定义两个相同类型的类的成员指针
- 将类成员指针传递给 Lambda 捕获列表 c++11
- 在单一实例类中将成员指针设置为 null 的正确方法是什么
- 无法删除在destructor中的成员指针的课程
- 删除对象而不调用成员指针的析构函数
- 当派生类的基类具有成员指针时,对其进行深层复制