-
September 21st, 2010, 05:40 AM
#1
Abstract class fundamentals
I understand we can't create objects of abstract class but how is the function "void prs( subt& i )" working. "subt& i" is passing by reference. But still we are passing by reference of an object of "subt" type which is an abstract class.
Here is the code:
Code:
#include <iostream>
#include <string>
using namespace std;
class subt
{
public:
virtual char * getd() = 0;
};
class sk:public subt
{
public:
char * getd() {return ( "in 1!" );
}
};
class ot:public subt
{
public:
char * getd() {return ( "in 2!" );
}
};
void prs( subt& i ) {printf( "%s\n", i.getd() );}
int main()
{ sk s;
ot o;
subt& g = o;
prs( s );
prs( o );
system("pause");
}
-
September 21st, 2010, 05:57 AM
#2
Re: Abstract class fundamentals
Passing a reference or a ptr to the abstract class to functions and methods is exactly the idea behind polymorphism.
A reference to the abstract class could be read as "a reference to any class derived from that abstract class" not knowing which class exactly that is.
You can then (inside the function prs) call all methods of the abstract class. The compiler ensures that there is at least one implementation available, because the reference passed to prs is not an instance of subt but an instance of a class derived from subt that implements (directly or indirectly) all abstract methods.
With regards
Programartist
-
September 21st, 2010, 06:29 AM
#3
Re: Abstract class fundamentals
I understand we can't create objects of abstract class but how is the function "void prs( subt& i )" working. "subt& i" is passing by reference. But still we are passing by reference of an object of "subt" type which is an abstract class.
Cannot see any contradiction. The objects is already created to the moment of referencing it. And passing by reference never implies creation of any sort.
Best regards,
Igor
-
September 21st, 2010, 07:10 AM
#4
Re: Abstract class fundamentals
Originally Posted by Igor Vartanov
The objects is already created to the moment of referencing it.
Can you please explain this?
Originally Posted by Igor Vartanov
And passing by reference never implies creation of any sort.
Does it mean objects would be created only when I use statements like:
int i;
and no object is created when I use:
int& i;
So, similarly no object is created when I use:
int* i; // as this just creates a reference to an object of type i even though the object does not exist yet?
-
September 21st, 2010, 08:42 AM
#5
Re: Abstract class fundamentals
Originally Posted by Learned
Does it mean objects would be created only when I use statements like:
int i;
and no object is created when I use:
int& i;
So, similarly no object is created when I use:
int* i; // as this just creates a reference to an object of type i even though the object does not exist yet?
Close, but there's subtleties here.
First, the compiler will not accept your second statement, since references must be initialized on creation and cannot be reassigned to point to something else later.
Second, your third statement is technically creating an object-----a pointer to int. It isn't creating an int, no, but the pointer is still a type of (primitive) object. Don't make the mistake of thinking pointers are somehow "special", they're just variables like everything else.
-
September 21st, 2010, 09:17 AM
#6
Re: Abstract class fundamentals
Originally Posted by Learned
Can you please explain this?
Sure. See below.
Does it mean objects would be created only when I use statements like:
int i;
Yes.
and no object is created when I use:
int& i;
Yes. A reference is a pointer in disguise. Syntactically it looks like a variable, but in fact it's a pointer which is automatically de-referenced (compiler will take care of that). In other words, reference is a pointer-that-is-always-dereferenced:
Code:
// the code...
int a = 5;
int& i = a; // This is it. The object 'a' is already created to the moment of referencing it.
int b = i;
i = 10;
// ... is in fact equivalent to
int a = 5;
int *i = &a;
int b = *i;
*i = 10;
So, similarly no object is created when I use:
int* i; // as this just creates a reference to an object of type i even though the object does not exist yet?
Not reference but pointer. Pointer itself is allowed to point or not to point to an existing variable, while reference always does refer to.
Last edited by Igor Vartanov; September 23rd, 2010 at 02:53 AM.
Best regards,
Igor
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|