why references are not used in virtual mechanism

Usually we use base class pointer and points to some derived object and use virtual mechanism.

For eg:
base* b = new derived;
b->virtualfunction();

here virtual function defined in derived class will be executed.


derived* d = new derived;
base& b= *d;
b.virtualfunction();
 
in this case also derived class function will be called.

why we are not using references in virtual mechanism...

please reply......
sebinfrancisAsked:
Who is Participating?
 
evilrixConnect With a Mentor Senior Software Engineer (Avast)Commented:
>> I just want to know then why it is not used normally in code( normal programming )

Right, but my point is you are making an assumption that references are not used and I am saying they are. The reasons for choosing one over the other a purely a design decision. What Paul gave you was one example for using pointers but one example does not prove your assumption. There are plenty of reasons for choosing pointers over references but there are also plenty of reasons for choosing references over pointers. Neither is a preferred paradigm and which you choose is purely down to the problem you are trying to solve.

Look at it this way, it's like asking why do programmers use signed rather than unsigned integers or floats over doubles. The question has no real basis of foundation because the simple fact is which you choose comes down to the problem you're facing and thus your choice will be defined by your problem domain and not just because you have a personal preference for one over the other. I might favour a hammer over a saw but I wouldn't bang in nails with a saw and I wouldn't cut wood with a hammer but both of them are used for construction purposes and I would use both to build a shed... it might be that I use the hammer more because I have more nails to band than wood to cut but that doesn't make it a better tool.

In general I prefer references over pointers because in simple terms they come with less baggage. There is no need to perform validation for null pointer and when you are doing a lot of generic programming using templates and static polymorphism mixed with dynamic polymorphism the calling syntax is uniform (ie, I don't need to care if I have a reference or an object as I can treat them identical whereas I need to partially specialise for a pointer). When I need to use pointer use them when I don't I prefer references. That doesn't mean either is better -- they are both just tools that should be used for the right reasons.

So, the fact is to choose just one example of using pointers rather than references is not a representative answer - it is one reason to choose pointers but it does not mean that in general this is what you would do. Put another way, I can only conclude you selected this as the solution because, ostensibly, it confirms your (incorrect) assumption. That; however, is a subjective rather than objective reason to choose this answer and as such means the answer that will be PAQ'd here is not really correct in regards to the point of this question (that's not to say what phoffric has said isn't correct with regards to one specific example).

The correct answer *is* that there is no definitive preference and it comes down to personal taste as a develop and will be defined by the problem space.

>> I am sorry if you misunderstood my question
I'm afraid you've misunderstood the use of pointers and references and more specifically my answer.
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> why we are not using references in virtual mechanism

I don't understand the question. You can use both pointers and references to a base class from a polymorphic derived class to get polymorphic behaviour. The choice it up to you as the programmer which you choose.
0
 
evilrixSenior Software Engineer (Avast)Commented:
Example.
#include <iostream>

struct animal
{
   virtual void noise() const = 0;
};

struct cat : animal
{
   void noise() const { std::cout << "meow" << std::endl; }
};

struct dog : animal
{
   void noise() const { std::cout << "bark" << std::endl; }
};

int main()
{
   animal const & a1 = cat();
   animal const & a2 = dog();

   a1.noise(); // meow
   a2.noise(); // bark
}

Open in new window

0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
sebinfrancisAuthor Commented:
normally we use base class pointers to get polymorphic behavior.  I have not seen anybody using references for this even though it also works. Is there any drawback for reference  while it is used in polymorphic calls.
0
 
evilrixSenior Software Engineer (Avast)Commented:
The behaviour semantics are identical only the syntax differs.
0
 
phoffricConnect With a Mentor Commented:
When using pointers you can change the behavior of the pointer by setting it to a different object. This characteristic is useful, for example, when receiving messages of different types. But when you define a reference, you are refering to a fixed object.
0
 
evilrixSenior Software Engineer (Avast)Commented:
sebinfrancis,

Is there any specific reason you chose to ignore what I said? I specifically stated there is no compelling reason to use pointers over references and that it was purely a developer decision. The behaviour from a polymorphic point of view is identical. The example phoffric has given is specifically a design decision of using pointers rather than references but from a pure virtual function point of view the semantics are are identical (ie. you will get polymorphic behaviour regardless of using a pointer of reference as long as there is a v-table).

In other words, the solution you've selected doesn't answer your specific question, it just gives an example of when you might choose a pointer over a reference but equally there are plenty of reasons why you might choose a reference over a pointer. The simple fact is it is a design decision taken by the developer depending upon circumstances but there is no compelling reason to choose one over the other from a polymorphic point of view. I specifically stated this in my first point and provided a working example in my second. It seems you have chosen an answer that backs your assumption rather than one that actually correctly answers the question.

-Rx.
0
 
sebinfrancisAuthor Commented:
hi
i also know reference supports polymorphic behaviors like pointer. I have stated that in my first post. I just want to know then why it is not used normally in code( normal programming )

I think phoffric correctly explained it.  I am sorry if you misunderstood my question.
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> when you are doing a lot of generic programming using templates and static polymorphism mixed with dynamic polymorphism the calling syntax is uniform.

As an example, the code below implements a (contrived but it is just to demonstrate the principle and is not an example of anything else) command pattern that mixes static polymorphism with dynamic polymorphism. In other words, you can prime the pattern with an object or reference to an object (with or without a polymorphic base class) and the behaviour is as expected in all circumstances, compiling without error and (in the case of dynamic types) calling the correct polymorphic override.

This code would be possible with pointers but because the calling syntax is different it would not compile without additional work to implement partial specialisations to accommodate the pointers calling syntax. Why would I bother when I can solve the same problem using just references and simpler, cleaner code?

In this case references provide a better solution (but not the only solution) in other cases pointers might be better. There is no one that is better than the other nor one that is generally preferred over the other (beyond programmer choice). The point is just because you can use pointers doesn't mean you would or should and vice versa... you choose the right tool for the job.
// this could also support pointers but it's just more work for no significant gain.

#include <iostream>

struct animal
{
   virtual void noise() const = 0;
};

struct cat : animal
{
   void noise() const { std::cout << "meow" << std::endl; }
};

struct dog : animal
{
   void noise() const { std::cout << "bark" << std::endl; }
};

struct machine
{
   virtual void noise() const = 0;
};

struct car : machine
{
   void noise() const { std::cout << "beep" << std::endl; }
};

struct van : machine
{
   void noise() const { std::cout << "roar" << std::endl; }
};

// A generic executor (representing something that might be used as part of the command design pattern).
// http://en.wikipedia.org/wiki/Command_pattern

template <typename T>
struct command
{
   command(T const & t) : t_(t)
   {
   }

   void operator()() const
   {
      t_.noise();
   }

   T const & t_;
};

void test_animal(animal & a)
{
   command<animal> cmd(a);
   cmd();
}

void test_machine(machine & m)
{
   command<machine> cmd(m);
   cmd();
}

template <typename T>
void test_generic(T const & t)
{
   command<T> cmd(t);
   cmd();
}

int main()
{
   cat c;
   dog d;
   car r;
   van v;

   command<animal> a1(c);
   command<animal> a2(d);
   command<machine> m1(r);
   command<machine> m2(v);

   a1();
   a2();
   m1();
   m2();

   command<cat> aa1(c);
   command<dog> aa2(d);
   command<car> mm1(r);
   command<van> mm2(v);

   aa1();
   aa2();
   mm1();
   mm2();

   test_animal(c);
   test_animal(d);

   test_machine(r);
   test_machine(v);

   test_generic(c);
   test_generic(d);
   test_generic(r);
   test_generic(v);
}

Open in new window

0
 
sebinfrancisAuthor Commented:
Hi evilrix,

Thank you very much for your detailed explanation.

Regards,
Sebin
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.