我从维基百科上读到:
"引用不能为null,而指针可以; 每个引用都指某个对象,尽管它可能有效也可能无效."
但我不相信因为下面的代码看看它编译器没有给出错误:
class person
{
public:
virtual void setage()=0;
};
int main()
{
person *object=NULL;
person &object1=*object;
}
Run Code Online (Sandbox Code Playgroud)
请详细说明这一点.
尊敬的先生!
我应该告诉你我所知道的和我不知道的问题,以便你能解决我理解的薄弱环节.
我知道c ++通过使用Vtable来实现多态性,Vtable是指针数组,每个指针指向类的虚函数,层次结构中的每个类都有一个vtable.现在假设我有以下课程
class person
{
char name[20];
public:
person(char* pname)
{
strcpy(name,pname);
}
virtual void show()
{
cout<<"inside person show method, Name: "<<name;
}
};
class teacher:public person
{
int scale;
teacher(char*pname, int s):person(pname)
{
scale=s;
}
void show()
{
cout<<"inside the teacher show method, Scale: "<<scale;
}
};
Run Code Online (Sandbox Code Playgroud)
现在假设我在主程序中写道
person *ptr;
ptr=new teacher(16,"Zia");
ptr->show();
Run Code Online (Sandbox Code Playgroud)
现在我很困惑,此时调用将转到基类的show函数,现在因为它是一个虚函数,因此它会调用approprite函数.我知道我错了.我很困惑,这将是一系列的电话.Vtable的作用是什么以及如何运作请详细说明.
我有以下课程
class Node
{
int key;
Node**Nptr;
public:
Node(int maxsize,int k);
};
Node::Node(int maxsize,int k)
{
//here i want to dynamically allocate the array of pointers of maxsize
key=k;
}
Run Code Online (Sandbox Code Playgroud)
请告诉我如何在构造函数中动态分配指针数组 - 此数组的大小为maxsize.
这个非常简单的代码块有问题.请给我你的建议. (我的这个问题已经解决了,在解决这个问题时,有id stakx的人确实对我有帮助,唯一的问题是我正在使用stack <treeNode>,当我仔细看到堆栈的推送方法时,有一个复制过程当我写head-> object = number时,最后我做了一堆指针,就像这个堆栈<treeNode*>它真的解决了问题,我现在没有问题,我非常非常感谢stakx.)
在代码之前你需要支持以下树的
替代文本http://img44.imageshack.us/img44/7016/avlimage06.jpg
,你可以在图片中看到root是8,堆栈有两个节点,即6和4.我将此堆栈和根节点传递给以下代码
void Avltree::attachwithtree(treeNode* tree, Stack<treeNode>&s)
{
if(!s.isempty())
{
treeNode *stacknode;
stacknode=s.pop();
cout<<"\ninside the attachwithtree function, stack node is "<<stacknode->data;
stacknode->right=tree;//attaching the passed node to the right of popped node
root=stacknode;//setting the root to stack node which is the private data member of class
updatebalance(root);//this function is ok, it does not create problem
while(!s.isempty())
{
cout<<"\nstack is still not empty";
stacknode=s.pop();
cout<<"\nright side of "<<root->data<<" is "<<(root->right)->data;
//the below three lines causing …Run Code Online (Sandbox Code Playgroud) 干草亲爱的!
我知道if语句在c ++中是一个昂贵的陈述.我记得,一旦我的老师说,如果声明在计算机时间意义上是一个昂贵的陈述.
现在我们可以通过在c ++中使用if语句来做所有事情,因此这在编程透视图中是非常强大的语句,但在计算机时间透视图中它很昂贵.
我是初学者,在介绍c ++课程后我正在学习数据结构课程.我的问题
是,我是否更好地使用if语句进行扩展?
我刚研究了不相交的集合数据结构,我知道它也被称为"联合查找数据结构",union和find是这个数据结构的两个主要操作.我们可以在不相交集上执行并集,类似地我们可以执行查找操作; 我想知道除了union和find之外我们可以在不相交集上执行的其他操作.
假设我有一个基类Person,我公开Teacher从基类继承一个类Person.现在在main函数中我写了这样的东西
// name will be passed to the base class constructor and 17
// is for derived class constructor.
Teacher object(“name”,17) ;
Teacher object1=object; //call to copy constructor
Run Code Online (Sandbox Code Playgroud)
现在我还没有为这两个类编写复制构造函数,当然会调用默认的复制构造函数.Person类的默认复制构造函数将首先调用基类的复制构造函数.
现在问题是假设我只为基类编写复制构造函数,会发生什么,派生类的默认复制构造函数将调用我的书写复制构造函数.
现在假设我为这两个类编写了复制构造函数.现在派生类(即教师)的复制构造函数将调用基类的默认构造函数而不是复制构造函数为什么?
只有派生类的默认复制构造函数可以自动调用基类的复制构造函数吗?
给定一个具有成员模板函数的类,如下所示:
template <typename t>
class complex
{
public:
complex(t r, t im);
template<typename u>
complex(const complex<u>&);
private:
//what would be the type of real and imaginary data members here.
}
Run Code Online (Sandbox Code Playgroud)
我对成员模板功能感到困惑,请提供一个示例,通过该示例,我可以清楚地了解成员模板功能的需求.
另外,告诉我在c ++中使用成员模板函数,我们使用成员模板函数的情况是什么?
我刚刚开始汇编语言编程,在第一堂课中,我们的老师告诉我们有关intel 8080和intel 8085的内容,他说这些处理器有64k内存.
现在我想知道我们如何使用特定处理器找到这么多的内存,例如我有一个1.8 Ghz的处理器,现在我怎么能找到可以用于这个处理器的内存量.
我想问的是告诉我如何找出这个内存量的方法?