POV-Ray : Newsgroups : povray.off-topic : Teach yourself C++ in 21 days Server Time
29 Jul 2024 20:22:30 EDT (-0400)
  Teach yourself C++ in 21 days (Message 99 to 108 of 168)  
<<< Previous 10 Messages Goto Latest 10 Messages Next 10 Messages >>>
From: Warp
Subject: Re: Days 1-5
Date: 20 Apr 2012 08:18:50
Message: <4f91542a@news.povray.org>
Darren New <dne### [at] sanrrcom> wrote:
> If you could #include <abc.o> you'd get rid of all the silliness of 
> declaring inline functions multiple times, function prototypes, and all that 
> kind of BS.

  So you would be simply changing the dependency to a .h file to dependency
to a .o file. What would be the relevant difference?

  Also, I assume you understand that templated code cannot be precompiled
into an object file? They need to be recompiled each time they are used...
which is kind of the whole idea.

-- 
                                                          - Warp


Post a reply to this message

From: Warp
Subject: Re: Days 1-5
Date: 20 Apr 2012 08:22:01
Message: <4f9154e9@news.povray.org>
Invisible <voi### [at] devnull> wrote:
> I'm currently trying to work out how I can compile a program where 
> module 1 refers to module 2, but module 2 also refers to module 1. As 
> far as I can figure out, this is impossible...

  The only thing that's impossible for a class to have another class
as member variable, and that another class having the first class as
member variable. (For obvious reasons. It would be an infinitely large
class.)

  Other than that, any kind of cross-referencing should be possible.

-- 
                                                          - Warp


Post a reply to this message

From: Warp
Subject: Re: Days 1-5
Date: 20 Apr 2012 08:29:48
Message: <4f9156bc@news.povray.org>
Alain <aze### [at] qwertyorg> wrote:
> >>     while (x[i] = y[i--]) ;

> Nothing undefined here.

> It's a simple loop that compare the array x[] and y[] untill it find a 
> value that is different.
> You get the value of x[i] and see if it's the same as the value of y[i] 
> then you decrement i by one.
> On exit, i will point to the value preceding the first different value.

  The compiler is free to decrement 'i' before it evaluates 'x[i]' or
after it. Hence you might get 'x[i]' as it was before the decrement, or
after. (The only thing that is guaranteed is that 'y[i]' is evaluated
before 'i' is decremented.) That's why it's undefined behavior.

-- 
                                                          - Warp


Post a reply to this message

From: Warp
Subject: Re: Day 6
Date: 20 Apr 2012 08:36:59
Message: <4f91586b@news.povray.org>
Orchid Win7 v1 <voi### [at] devnull> wrote:
> On the other hand, it appears to be aimed at people who already know the 
> C++ language. That isn't really me, yet. I know some of the syntax, but 
> certainly not all of it, or what all of it actually does.

  I think you'll learn on the fly (at least if you have the right
attitude).

-- 
                                                          - Warp


Post a reply to this message

From: Invisible
Subject: Re: Days 1-5
Date: 20 Apr 2012 09:02:12
Message: <4f915e54$1@news.povray.org>
>> I'm currently trying to work out how I can compile a program where
>> module 1 refers to module 2, but module 2 also refers to module 1. As
>> far as I can figure out, this is impossible...
>
>    The only thing that's impossible for a class to have another class
> as member variable, and that another class having the first class as
> member variable.

Yeah, that's exactly what I'm trying to do.

Actually, no, wait... Class X has class Y as a member, but class Y only 
/mentions/ class X. (It has member functions that return objects of 
class X.)

> (For obvious reasons. It would be an infinitely large class.)

It can if at least one of the classes refers to the other through a 
pointer or a reference. (Think about it: A binary tree class has no 
problem containing /itself/ as a member variable or two.)

So, the way I see it, I have two options:

1. Put class X and class Y in the same header file.

2. Start throwing unchecked casts around the place.


Post a reply to this message

From: Invisible
Subject: Re: Day 6
Date: 20 Apr 2012 09:03:43
Message: <4f915eaf$1@news.povray.org>
On 20/04/2012 01:36 PM, Warp wrote:
> Orchid Win7 v1<voi### [at] devnull>  wrote:
>> On the other hand, it appears to be aimed at people who already know the
>> C++ language. That isn't really me, yet. I know some of the syntax, but
>> certainly not all of it, or what all of it actually does.
>
>    I think you'll learn on the fly (at least if you have the right
> attitude).

Given that I already some some clue about C++, that might actually work...


Post a reply to this message

From: Warp
Subject: Re: Days 1-5
Date: 20 Apr 2012 10:15:30
Message: <4f916f82@news.povray.org>
Invisible <voi### [at] devnull> wrote:
> >    The only thing that's impossible for a class to have another class
> > as member variable, and that another class having the first class as
> > member variable.

> Yeah, that's exactly what I'm trying to do.

  I don't think so.

> Actually, no, wait... Class X has class Y as a member, but class Y only 
> /mentions/ class X. (It has member functions that return objects of 
> class X.)

  Sounds like a rather complicated design. Anyways, you can declare classes
(as opposed to defining them). In other words, you can do this:

//----------------------------------------
// This could be eg. in a "Y.hh" header file
class X; // declaration of X

class Y
{
 public:
    void foo() { std::cout << "I'm an Y\n"; }
    X gimmeAnX();
};
//----------------------------------------

  You can then define X for example like:

//----------------------------------------
// This could be eg. in a "X.hh" header file,
// in which case you would have a
// #include "Y.hh" here.
class X
{
    Y anObjectOfY;

 public:
    void bar() { anObjectOfY.foo(); }
};
//----------------------------------------

  After both definitions you can now implement the Y::gimmeAnX() function,
eg. like:

//----------------------------------------
// This could be eg. in a "Y.cc" source file,
// in which case you would have a
// #include "X.hh" here.
X Y::gimmeAnX() { return X(); }
//----------------------------------------

  (All of the above code could be in one single source file as well,
in that order, which is why I didn't explicitly use any #include lines.)

> > (For obvious reasons. It would be an infinitely large class.)

> It can if at least one of the classes refers to the other through a 
> pointer or a reference.

  It wouldn't be a member variable then.

> So, the way I see it, I have two options:

> 1. Put class X and class Y in the same header file.

  Not necessary (and even if you put it in the same header file, that alone
wouldn't solve your problem).

> 2. Start throwing unchecked casts around the place.

  The correct solution is to use class declarations.

-- 
                                                          - Warp


Post a reply to this message

From: Invisible
Subject: Re: Days 1-5
Date: 20 Apr 2012 10:19:53
Message: <4f917089$1@news.povray.org>
On 20/04/2012 03:15 PM, Warp wrote:

>    Sounds like a rather complicated design. Anyways, you can declare classes
> (as opposed to defining them). In other words, you can do this:
>
> //----------------------------------------
> // This could be eg. in a "Y.hh" header file
> class X; // declaration of X

Ah, I see. This is the fact I was looking for. :-)

So this does... what? Tells the compiler "hey, this type exists, but I'm 
not going to tell you anything about it"?

>>> (For obvious reasons. It would be an infinitely large class.)
>
>> It can if at least one of the classes refers to the other through a
>> pointer or a reference.
>
>    It wouldn't be a member variable then.

What would it be then?

>> 1. Put class X and class Y in the same header file.
>
>    Not necessary (and even if you put it in the same header file, that alone
> wouldn't solve your problem).

Quite right. That doesn't help at all...


Post a reply to this message

From: Warp
Subject: Re: Days 1-5
Date: 20 Apr 2012 10:29:32
Message: <4f9172cc@news.povray.org>
Invisible <voi### [at] devnull> wrote:
> > class X; // declaration of X

> Ah, I see. This is the fact I was looking for. :-)

> So this does... what? Tells the compiler "hey, this type exists, but I'm 
> not going to tell you anything about it"?

  It tells that there's a class named X.

  From that point forward you can do anything with that type that does not
require calling its member functions or accessing its contents. (In other
words, you can handle pointers and references of that type.) You can also
make further declarations using it (such as declaring it as the return
type of a function).

  Doing anything else with an object of that type requires a full class
definition, but that can be done later in the code.

  This allows inter-dependency between classes.

> >>> (For obvious reasons. It would be an infinitely large class.)
> >
> >> It can if at least one of the classes refers to the other through a
> >> pointer or a reference.
> >
> >    It wouldn't be a member variable then.

> What would it be then?

  A pointer/reference to an object of that type.

  What I mean is that you can't do this:

class A { B b; };
class B { A a; };

  That would be a pair of infinitely large classes.

-- 
                                                          - Warp


Post a reply to this message

From: Warp
Subject: Re: Days 1-5
Date: 20 Apr 2012 10:34:22
Message: <4f9173ee@news.povray.org>
Warp <war### [at] tagpovrayorg> wrote:
>   From that point forward you can do anything with that type that does not
> require calling its member functions or accessing its contents.

  (Oh, and of course anything requiring knowing the size of X doesn't work
with the declaration only, of course. For instance, "sizeof(X)" obviously
doesn't work without the full class definition.)

-- 
                                                          - Warp


Post a reply to this message

<<< Previous 10 Messages Goto Latest 10 Messages Next 10 Messages >>>

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.