|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
1.
---code-- class base{ public: virtual void func(); }; class derive:public base{ virtual void func(); //1 }; ---code-- As we know that Line 1 implements the function overloading, what's the difference between "virtual void func();" and "void func();" in L1? 2.why people sometimes define constructor/destructor virtual? what if a pure virtual constructor/destructor? 3. --code-- int *x = new int[0]; cout<<x<<endl; --code-- the result is not 0, what happened? 4. when calling "delete []p;", how does the program know how many elements should be destroyed? |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
thomas <FreshThomas@gmail.com> writes:
> 1. > ---code-- > class base{ > public: > virtual void func(); > }; > class derive:public base{ > virtual void func(); //1 > }; > ---code-- > As we know that Line 1 implements the function overloading, > what's the difference between "virtual void func();" and "void > func();" in L1? That's the same difference as with 'final' methods in java. > 2.why people sometimes define constructor/destructor virtual? > what if a pure virtual constructor/destructor? constructor can't be defined as virtual AFAIK. destructurs must be defined as virtual as soon as there is a virtual method, to allow calling the right destructor (that is, the one of the exact class of the object), when delete is called with a pointer to the object typed as a superclass: class A { ResourceA* ra; A(){ra=ra_alloc();} virtual ~A(){ ra_free(ra); } }; class B { ResourceB* rb B(){rb=rb_alloc();} virtual ~B(){ rb_free(rb); } }; int main(){ A* obj=new B(); delete obj; // <-- we want ~B to be called too here! return 0; } > 3. > --code-- > int *x = new int[0]; > cout<<x<<endl; > --code-- > the result is not 0, what happened? There is only one null pointer. > 4. when calling "delete []p;", how does the program know how many > elements should be destroyed? new[] stores the size allocated in the allocated memory block. Since these are probably homework, you won't have learned anything, and be sure that any employer will detect it in the first five minute of the interview. You probably won't get a job in C++ programming... Next time, do your own homework! -- __Pascal Bourguignon__ |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
> > As we know that Line 1 implements the function overloading,
> > what's the difference between "virtual void func();" and "void > > func();" in L1? > > That's the same difference as with 'final' methods in java. I guess you mean that this method cannot be overloaded by child classes if not defined virtual. > constructor can't be defined as virtual AFAIK. destructurs must be > defined as virtual as soon as there is a virtual method, to allow > calling the right destructor (that is, the one of the exact class of > the object), when delete is called with a pointer to the object typed > as a superclass: > int main(){ > A* obj=new B(); > delete obj; // <-- we want ~B to be called too here! > return 0; > } I think I got it! another problem: --code-- class A{ virtual int func(); }; class B{ virtual double func(); //is this overloading? }; > > 3. > > --code-- > > int *x = new int[0]; > > cout<<x<<endl; > > --code-- > > the result is not 0, what happened? > > There is only one null pointer. So the x will always get one piece memory allocated? Then when will it be freed? If "delete []x" is never called, will it never be freed? > > > 4. when calling "delete []p;", how does the program know how many > > elements should be destroyed? > > new[] stores the size allocated in the allocated memory block. How is the memory organized? Is the first element any different(store the size) with others? Why calling "delete p" will not free all the memory if size is known? > > Since these are probably homework, you won't have learned anything, > and be sure that any employer will detect it in the first five minute > of the interview. You probably won't get a job in C++ programming... > > Next time, do your own homework! Thanks for your reply. Actually I'm self-learning c++, just want some details clarified. I desire to master the language, and am afraid of having no job after graduation, so I gona work harder. :-) > > -- > __Pascal Bourguignon__ |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
> > class A { > ResourceA* ra; > A(){ra=ra_alloc();} > virtual ~A(){ > ra_free(ra); > } > > }; > > class B { > ResourceB* rb > B(){rb=rb_alloc();} > virtual ~B(){ > rb_free(rb); > } > > }; > > int main(){ > A* obj=new B(); > delete obj; // <-- we want ~B to be called too here! > return 0; > The destructor overloading seems quite strange. "virtual ~A();" in A, and "virtual ~B();" in B. have different function names. |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
In message
<31485ab5-29e2-4edd-9c81-f21f7d64f1ca@i29g2000prf.googlegroups.com>, thomas <FreshThomas@gmail.com> writes >> > As we know that Line 1 implements the function overloading, >> > what's the difference between "virtual void func();" and "void >> > func();" in L1? >> >> That's the same difference as with 'final' methods in java. > >I guess you mean that this method cannot be overloaded by child >classes if not defined virtual. If so, he's wrong. C++ has no direct equivalent to "final". The "virtual" keyword in the derived class is redundant. It makes no difference to the semantics of the class, but it may to document it. -- Richard Herring |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
thomas <FreshThomas@gmail.com> wrote:
> 1. > ---code-- > class base{ > public: > virtual void func(); > }; > class derive:public base{ > virtual void func(); //1 > }; > ---code-- > As we know that Line 1 implements the function overloading, > what's the difference between "virtual void func();" and "void > func();" in L1? There is none. At line 1, the 'virtual' keyword is optional. > 2.why people sometimes define constructor/destructor virtual? Nobody ever defines a constructor as virtual. Destructors are defined as virtual so the base destructor will be called when a pointer to derived is deleted. > what if a pure virtual constructor/destructor? Destructors must be defined. You can have this: class Foo { public: virtual ~Foo() = 0; }; but you still have to define Foo::~Foo() somewhere. What the above does is make the class abstract, even if all other member-functions are defined. > 3. > --code-- > int *x = new int[0]; > cout<<x<<endl; > --code-- > the result is not 0, what happened? 'new' is guaranteed to return memory or throw. In the above, you newed a block of memory of unspecified size, that can't be accessed for any reason. "*x" would be undefined behavior. > 4. when calling "delete []p;", how does the program know how many > elements should be destroyed? How the system does unspecified by the language. |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
thomas <FreshThomas@gmail.com> wrote:
> > > As we know that Line 1 implements the function overloading, > > > what's the difference between "virtual void func();" and "void > > > func();" in L1? > > > > That's the same difference as with 'final' methods in java. > > I guess you mean that this method cannot be overloaded by child > classes if not defined virtual. That is incorrect. It was defined virtual in the base class, so it can be overridden by any derived class, even if some derived class in the chain doesn't use the virtual keyword. For example: class A { public: virtual int foo(); }; class B : public A { public: int foo(); }; class C : public B { public: int foo(); }; int main() { C c; A* a = &c; a->foo(); // C::foo() will be called } > > constructor can't be defined as virtual AFAIK. destructurs must be > > defined as virtual as soon as there is a virtual method, to allow > > calling the right destructor (that is, the one of the exact class of > > the object), when delete is called with a pointer to the object typed > > as a superclass: > > int main(){ > > A* obj=new B(); > > delete obj; // <-- we want ~B to be called too here! > > return 0; > > } > I think I got it! > another problem: > --code-- > class A{ > virtual int func(); > }; > class B{ > virtual double func(); //is this overloading? > }; No. "Two functions that appear in the same scope are overloaded if they have the same name but have different parameter list." The above two functions are not in the same scope and don't have a different parameter list, so this is not an example of overloading. > > > 3. > > > --code-- > > > int *x = new int[0]; > > > cout<<x<<endl; > > > --code-- > > > the result is not 0, what happened? > > > > There is only one null pointer. > > So the x will always get one piece memory allocated? Correct. 'new' always returns memory, or throws. > If "delete []x" is never called, will it never be freed? Correct. > > > 4. when calling "delete []p;", how does the program know how many > > > elements should be destroyed? > > > > new[] stores the size allocated in the allocated memory block. > > How is the memory organized? However the system wants. The organization isn't specified by the standard. > Is the first element any different(store > the size) with others? No. > Why calling "delete p" will not free all the memory if size is known? Calling delete on memory that was allocated using new[] is undefined. It might release all the memory, it might not. It might call all the destructors, it might not. It might crash the program, it might not... &c. |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
Pascal Bourguignon wrote:
> thomas <FreshThomas@gmail.com> writes: > >> 1. >> ---code-- >> class base{ >> public: >> virtual void func(); >> }; >> class derive:public base{ >> virtual void func(); //1 >> }; >> ---code-- >> As we know that Line 1 implements the function overloading, >> what's the difference between "virtual void func();" and "void >> func();" in L1? > > That's the same difference as with 'final' methods in java. No. Daniel is right. It is just optional. Example: #include <stdio.h> struct A { virtual void foo() { puts("A::foo()"); } }; struct B : public A { void foo() { puts("B::foo()"); } }; struct C : public B { void foo() { puts("C::foo()"); } }; int main() { A* x = new C; x->foo(); delete x; return 0; } Marcel |
|
![]() |
| Outils de la discussion | |
|
|