PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > some puzzles
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
some puzzles

Réponse
 
LinkBack Outils de la discussion
Vieux 22/02/2008, 08h58   #1
thomas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut some puzzles

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?
  Réponse avec citation
Vieux 22/02/2008, 09h06   #2
Pascal Bourguignon
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: some puzzles

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__
  Réponse avec citation
Vieux 22/02/2008, 09h22   #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
Vieux 22/02/2008, 09h25   #4
thomas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: some puzzles


>
> 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.
  Réponse avec citation
Vieux 22/02/2008, 11h39   #5
Richard Herring
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: some puzzles

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
  Réponse avec citation
Vieux 22/02/2008, 12h19   #6
Daniel T.
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: some puzzles

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.
  Réponse avec citation
Vieux 22/02/2008, 12h31   #7
Daniel T.
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: some puzzles

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.
  Réponse avec citation
Vieux 22/02/2008, 15h12   #8
Marcel Müller
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: some puzzles

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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 13h35.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,22124 seconds with 16 queries