CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2009
    Location
    NY, USA
    Posts
    191

    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");
    }

  2. #2
    Join Date
    May 2006
    Location
    Dresden, Germany
    Posts
    458

    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

  3. #3
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    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

  4. #4
    Join Date
    Oct 2009
    Location
    NY, USA
    Posts
    191

    Re: Abstract class fundamentals

    Quote Originally Posted by Igor Vartanov View Post
    The objects is already created to the moment of referencing it.
    Can you please explain this?

    Quote Originally Posted by Igor Vartanov View Post
    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?

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Abstract class fundamentals

    Quote Originally Posted by Learned View Post
    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.

  6. #6
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Abstract class fundamentals

    Quote Originally Posted by Learned View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured