也是一个继承问题 [英] also an inheritance problem
问题描述
大家好,
我是C +的新手。我遇到了一个问题。有人可以帮助我吗?
我有基础课:
class a
{
受保护:
int b;
public:
int getint(){return b;};
setint (int a){b = a};
};
class b:public a
{
.......
}
class c:public a
{
} >
main
{
int k;
b * p1;
c * p2;
p1-> setint(3);
k = p2-> getint();
}
为什么k的值不是3?请帮我。怎么能这样做。谢谢
Hi all,
I am really new in C++. I met a problem. Could someone help me?
I have a base class:
class a
{
protected:
int b;
public:
int getint(){return b;};
setint (int a){ b=a};
};
class b:public a
{
.......
}
class c:public a
{
}
main
{
int k;
b *p1;
c *p2;
p1->setint(3);
k=p2->getint();
}
why the value of k is not 3? please help me. how can do that. thanks
推荐答案
David写道:
David wrote:
大家好,
我是C ++的新手。我遇到了一个问题。有人可以帮助我吗?
我有基础课:
class a
{
受保护:
int b;
public:
int getint(){return b;};
setint (int a){b = a};
};
class b:public a
{
......
}
class c:public a
{
}
main
{
int k;
b * p1;
c * p2 ;
p1-> setint(3);
k = p2-> getint();
}
为什么k的值不是3?请帮我。怎么能这样做。谢谢
Hi all,
I am really new in C++. I met a problem. Could someone help me?
I have a base class:
class a
{
protected:
int b;
public:
int getint(){return b;};
setint (int a){ b=a};
};
class b:public a
{
......
}
class c:public a
{
}
main
{
int k;
b *p1;
c *p2;
p1->setint(3);
k=p2->getint();
}
why the value of k is not 3? please help me. how can do that. thanks
即使我们删除所有语法错误,你仍然有一个非常糟糕的问题
那里:p1和p2指针实际上都没有指向任何东西。
尝试通过指向一个实际(有效)对象的指针来调用成员函数具有未定义的行为。
V
-
请在通过电子邮件回复时删除资金''A'
我没有回复最热门的回复,请不要问
Even if we remove all syntax errors, you still have a very bad problem
there: both p1 and p2 pointers do not actually point to anything.
Trying to call a member function through a pointer that doesn''t point
to a real (valid) object has undefined behaviour.
V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask
David写道:
David wrote:
大家好,
我是C +的新手。我遇到了一个问题。有人可以帮助我吗?
我有基础课:
class a
{
受保护:
int b;
public:
int getint(){return b;};
setint (int a){b = a};
};
class b:public a
{
......
}
class c:public a
{
}
main
{
int k;
b * p1;
c * p2 ;
p1-> setint(3);
k = p2-> getint();
}
为什么k的值不是3?请帮我。怎么能这样做。谢谢
Hi all,
I am really new in C++. I met a problem. Could someone help me?
I have a base class:
class a
{
protected:
int b;
public:
int getint(){return b;};
setint (int a){ b=a};
};
class b:public a
{
......
}
class c:public a
{
}
main
{
int k;
b *p1;
c *p2;
p1->setint(3);
k=p2->getint();
}
why the value of k is not 3? please help me. how can do that. thanks
指针不是对象。一个指针只不过是一个指向什么都没有的地址
。
邮件员不会建造一个房子,因为你发了一封信给
虚构地址。
汽车牌照不属于汽车,直到你把它放在
汽车上。
#include< iostream>
A级
{
int a;
public:
A():a(0){} //默认构造函数
int get()const {return a; }
void set(int n){a = n; }
};
int main()
{
一个实例; //一个对象
std :: cout<< "& instance =" << & instance<< std :: endl;
A * p_a; //一个没有指针的指针
p_a = 0; //空指针
std :: cout<< pointer p_a =" << p_a<< std :: endl;
p_a =& instance; // p_a现在实际指向真实的东西
std :: cout<< pointer p_a =" << p_a<< std :: endl;
p_a-> set(3);
std :: cout<< " p_a> get()=" << p_a> get()<< std :: endl;
int k = p_a> get();
std :: cout<< k = << k<< std :: endl;
}
/ *
& instance = 0x7fff42d853c0
指针p_a = 0
指针p_a = 0x7fff42d853c0
p_a-> get()= 3
k = 3
* /
每当你声明指针时,记得要始终初始化它。
A a;
A * ptr = 0; //空指针
ptr =& a; //指向对象的有效指针
Pointers are not objects. A pointer is nothing more than an address
that points to nothing.
The mailman will not construct a home because you sent a letter to a
fictitious address.
A car''s license plate does not belong to a car until you put it on the
car.
#include <iostream>
class A
{
int a;
public:
A() : a(0) { } // default constructor
int get() const { return a; }
void set(int n) { a = n; }
};
int main()
{
A instance; // an object
std::cout << "&instance = " << &instance << std::endl;
A* p_a; // a pointer to nothing
p_a = 0; // a null pointer
std::cout << "pointer p_a = " << p_a << std::endl;
p_a = &instance; // p_a now actually points to something real
std::cout << "pointer p_a = " << p_a << std::endl;
p_a->set( 3 );
std::cout << "p_a->get() = " << p_a->get() << std::endl;
int k = p_a->get();
std::cout << "k = " << k << std::endl;
}
/*
&instance = 0x7fff42d853c0
pointer p_a = 0
pointer p_a = 0x7fff42d853c0
p_a->get() = 3
k = 3
*/
Whenever you declare a pointer, remember to always initialize it.
A a;
A* ptr = 0; // null pointer
ptr = &a; // a valid pointer to an object
David wrote:
大家好,
b * p1;
Hi all,
b *p1;
....
....
p1-> setint(3);
p1->setint(3);
合理的编译器会警告未初始化的指针。你有没有编译这段代码?
Best,Dan。
A reasonable compiler would have warned the uninitialized pointer. Have
you compiled this code?
Best, Dan.
这篇关于也是一个继承问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!