Discussion: some puzzles
Afficher un message
Vieux 22/02/2008, 10h22   #3
thomas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: some puzzles

> > 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__


  Réponse avec citation
 
Page generated in 0,05960 seconds with 9 queries