vc++ class need run-time library?

Programming, for all ages and all languages.
hojjatrakhshani
Posts: 21
Joined: Sun Feb 19, 2012 7:25 am

vc++ class need run-time library?

Post by hojjatrakhshani »

hi!
mvc++ class need run-time library?
thank!
evoex
Member
Member
Posts: 103
Joined: Tue Dec 13, 2011 4:11 pm

Re: vc++ class need run-time library?

Post by evoex »

berkus wrote:hi!
class need library sometimes.
pleas
hi!
I haz lol'ed.
thank!
hojjatrakhshani
Posts: 21
Joined: Sun Feb 19, 2012 7:25 am

Re: vc++ class need run-time library?

Post by hojjatrakhshani »

excusme if my question was very bad.
there for in normal condition i didnt need run time library except when use for example pur virtual function.yes it is?
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: vc++ class need run-time library?

Post by Solar »

+++ More Cheese Error +++

Insufficient information.

Redo from start.
Every good solution is obvious once you've found it.
User avatar
Griwes
Member
Member
Posts: 374
Joined: Sat Jul 30, 2011 10:07 am
Libera.chat IRC: Griwes
Location: Wrocław/Racibórz, Poland
Contact:

Re: vc++ class need run-time library?

Post by Griwes »

berkus wrote:1) they are pure and therefore are not implemented
You know that pure virtual function can have implementation, don't you? ;D
Reaver Project :: Repository :: Ohloh project page
<klange> This is a horror story about what happens when you need a hammer and all you have is the skulls of the damned.
<drake1> as long as the lock is read and modified by atomic operations
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: vc++ class need run-time library?

Post by Solar »

Griwes wrote:
berkus wrote:1) they are pure and therefore are not implemented
You know that pure virtual function can have implementation, don't you? ;D
I'm not sure which strange programming language you are thinking about, but in C++, a pure virtual function declaration looks like this:

Code: Select all

virtual void virtualfunctioname() = 0;
The "= 0" indicates that this function does not have an implementation, which - at least in C++ - is the definition of "pure virtual". Not only can the function not have an implementation, the whole class cannot be instantiated.
Every good solution is obvious once you've found it.
cyr1x
Member
Member
Posts: 207
Joined: Tue Aug 21, 2007 1:41 am
Location: Germany

Re: vc++ class need run-time library?

Post by cyr1x »

Solar wrote:

Code: Select all

virtual void virtualfunctioname() = 0;
The "= 0" indicates that this function does not have an implementation, which - at least in C++ - is the definition of "pure virtual". Not only can the function not have an implementation, the whole class cannot be instantiated.
I think you are wrong here. A pure virtual function can have an implementation, still the class cannot be instantiated, but the function can be called by derived classes.

Code: Select all

virtual void func() = 0
{ std::cout << "HELLO WORLD"; }
hojjatrakhshani
Posts: 21
Joined: Sun Feb 19, 2012 7:25 am

Re: vc++ class need run-time library?

Post by hojjatrakhshani »

berkus wrote: Are you trying to approach C++ programming without actually understanding what C++ is and how it works? This approach is bound to repetitive fail.
can you give me book or some resource aboute this?
best regard
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: vc++ class need run-time library?

Post by Solar »

cyr1x wrote: I think you are wrong here.
I think I am not. Stroustrup thinks I am not. GCC thinks I am not:

Code: Select all

solar@valkyrie ~ $ cat test.cpp 
#include <iostream>

class AbstractClass
{
    public:
        virtual void func() = 0
        {
            std::cout << "Hello World" << std::endl;
        }
};
solar@valkyrie ~ $ g++ -c test.cpp -o test.o
test.cpp:6:29: error: pure-specifier on function-definition
The whole point of a pure virtual function is that you cannot define the function for a base class, because there is no sensible definition in the base class. By making the function "pure virtual" (and thus, the base class an abstract class), you achieve two things: 1) You make sure that the base class cannot be instantiated, and 2) you make sure that any derived class that could be instantiated must have overloaded that pure virtual function (or the derived class would, in turn, be abstract).

Trying to define a function body and declaring the function pure virtual is showing some degree of ignorance of the construct.
Last edited by Solar on Sun Jun 03, 2012 3:21 am, edited 1 time in total.
Every good solution is obvious once you've found it.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: vc++ class need run-time library?

Post by Solar »

hojjatrakhshani wrote:
berkus wrote: Are you trying to approach C++ programming without actually understanding what C++ is and how it works? This approach is bound to repetitive fail.
can you give me book or some resource aboute this?
Stroustrup, "The C++ Programming Language". There are others, perhaps even better ones, but this one is "from the horse's mouth" as Stroustrup is the one who invented the language in the first place.

One thing though... as a beginner in a programming language, osdev.org is the wrong place to hang out.

PS: Improving your English would also be a definite bonus.
Every good solution is obvious once you've found it.
cyr1x
Member
Member
Posts: 207
Joined: Tue Aug 21, 2007 1:41 am
Location: Germany

Re: vc++ class need run-time library?

Post by cyr1x »

OK the above snippet was only accepted by Visual Studio.
With GCC this works for me at least:

Code: Select all

class pure
{
public:
    virtual void func() = 0;
};

void pure::func() {
    std::cout << "Hello world";
}
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: vc++ class need run-time library?

Post by Solar »

What are you trying to achieve?

Even if you find a way to get this past the compiler, what do you think you're doing?

Not everything that compiles is legal, let alone "good code". I'm quite confused that this construct compiles, because it doesn't make any sense.
Every good solution is obvious once you've found it.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: vc++ class need run-time library?

Post by Solar »

PS: Found this one on StackOverflow. Apparently it is actually allowable, enabling the abstract base class definition to be called explicitly. Quoting the accepted answer:
The use case I can think of off the top of my head is when there's a more-or-less reasonable default behavior, but the class designed wants that sort-of-default behavior to be invoked only explicitly. It can also be the case what you want derived classes to always perform their own work but also be able to call a common set of functionality.

Note that even though it's permitted by the language, it's not something that I see commonly used (and the fact that it can be done seems to surprise most C++ programmers, even experienced ones).
I second the last statement. I consider myself a war-hardened C++ veteran, and have seen much in my time. You see from the above how much this construct surprised me - and I still think it's a somewhat broken approach.

Not that this ever stopped C++ from doing it anyway. :twisted:

Congrats on this one. It's not often that anyone catches me flat-footed in a C/C++ topic.
Every good solution is obvious once you've found it.
cyr1x
Member
Member
Posts: 207
Joined: Tue Aug 21, 2007 1:41 am
Location: Germany

Re: vc++ class need run-time library?

Post by cyr1x »

Actually this thing surprised me too when I first encountered it.
User avatar
iansjack
Member
Member
Posts: 4687
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: vc++ class need run-time library?

Post by iansjack »

I'm probably being really stupid, so I apologise in advanced, but once you supply a function definition other than = 0 surely it is no longer a pure virtual function? The same way that a null pointer is no longer a null pointer when you assign a value to it.

As I say, it's probably a stupid remark, so please don't flame me.
Post Reply