Page 1 of 2

vc++ class need run-time library?

Posted: Sat Jun 02, 2012 8:33 am
by hojjatrakhshani
hi!
mvc++ class need run-time library?
thank!

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

Posted: Sat Jun 02, 2012 9:10 am
by evoex
berkus wrote:hi!
class need library sometimes.
pleas
hi!
I haz lol'ed.
thank!

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

Posted: Sat Jun 02, 2012 12:20 pm
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?

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

Posted: Sat Jun 02, 2012 12:25 pm
by Solar
+++ More Cheese Error +++

Insufficient information.

Redo from start.

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

Posted: Sat Jun 02, 2012 3:03 pm
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

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

Posted: Sun Jun 03, 2012 12:49 am
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.

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

Posted: Sun Jun 03, 2012 1:57 am
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"; }

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

Posted: Sun Jun 03, 2012 2:05 am
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

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

Posted: Sun Jun 03, 2012 3:12 am
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.

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

Posted: Sun Jun 03, 2012 3:14 am
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.

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

Posted: Sun Jun 03, 2012 3:46 am
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";
}

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

Posted: Sun Jun 03, 2012 3:52 am
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.

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

Posted: Sun Jun 03, 2012 4:03 am
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.

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

Posted: Sun Jun 03, 2012 4:10 am
by cyr1x
Actually this thing surprised me too when I first encountered it.

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

Posted: Sun Jun 03, 2012 4:55 am
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.