POV-Ray : Newsgroups : povray.off-topic : C++ questions Server Time
7 Sep 2024 21:12:59 EDT (-0400)
  C++ questions (Message 24 to 33 of 123)  
<<< Previous 10 Messages Goto Latest 10 Messages Next 10 Messages >>>
From: Warp
Subject: Re: C++ questions
Date: 25 Sep 2008 18:33:04
Message: <48dc11a0@news.povray.org>
Darren New <dne### [at] sanrrcom> wrote:
> Correct. I was also pointing out that they get initialized to the type's 
> version of zero, whatever that might be. (I.e., pointers may not have 
> all zero bits in them; they get initialized to null instead.) Rarely can 
> you see this difference. :-)

  Actually it's possible to invoke the "default initialization" for any
type at any point in C++. For example if you do this:

    int i = int();

the 'i' variable will be default-initialized with the "zero" value for
the type in question (which, in this case, is the integer 0).

  One might ask what's the point. It's actually useful when you don't
know what the type actually is. This happens eg. with templates. You
could have, for example, this:

template<typename Type>
void foo()
{
    Type i = Type(); // Default-initialize i, even if it's a basic type
    ...
}

  Although mostly it's useful in situations where you want to create a
default-initialized temporary, for example in code like this:

    someVector.push_back(Type());

  That will work even if 'Type' is an int, a double or a pointer.

-- 
                                                          - Warp


Post a reply to this message

From: Slime
Subject: Re: C++ questions
Date: 26 Sep 2008 01:38:47
Message: <48dc7567$1@news.povray.org>
> Which timezone is that then?

PST. (California.)

 - Slime
 [ http://www.slimeland.com/ ]


Post a reply to this message

From: Invisible
Subject: Re: C++ questions
Date: 26 Sep 2008 04:26:48
Message: <48dc9cc8$1@news.povray.org>
Slime wrote:
>> Which timezone is that then?
> 
> PST. (California.)

You're Californian?

Wow... suddenly a whole lot of POV-Ray stuff makes way more sense... ;-)


Post a reply to this message

From: Invisible
Subject: Re: C++ questions
Date: 26 Sep 2008 07:27:22
Message: <48dcc71a$1@news.povray.org>
Copy...constructor...? x_x

Heh. And I thought figurating out the difference between a const pointer 
and a pointer to a const was hard!


Post a reply to this message

From: Invisible
Subject: Re: C++ questions
Date: 26 Sep 2008 07:50:16
Message: <48dccc78@news.povray.org>
Invisible wrote:
> Copy...constructor...? x_x

Pointers to members?

OK, maybe I should stop reading now. :-/


Post a reply to this message

From: Warp
Subject: Re: C++ questions
Date: 26 Sep 2008 08:58:06
Message: <48dcdc5e@news.povray.org>
Invisible <voi### [at] devnull> wrote:
> Copy...constructor...? x_x

  Usually you don't need copy constructors unless you have pointers as
member variables of your class and you explicitly allocate memory to
them with 'new'. But we don't do that, do we? ;)

  If you want a quick crash course on copy constructing when the class
allocates memory explicitly, check this:

http://warp.povusers.org/c++test/

  (There are actually a few other situations where a copy constructor is
mandatory for a class to work properly, but they are slightly advanced
stuff.)

-- 
                                                          - Warp


Post a reply to this message

From: Warp
Subject: Re: C++ questions
Date: 26 Sep 2008 09:00:02
Message: <48dcdcd2@news.povray.org>
Invisible <voi### [at] devnull> wrote:
> Pointers to members?

  You don't need them.

-- 
                                                          - Warp


Post a reply to this message

From: Invisible
Subject: Re: C++ questions
Date: 26 Sep 2008 09:07:26
Message: <48dcde8e$1@news.povray.org>
Warp wrote:
> Invisible <voi### [at] devnull> wrote:
>> Pointers to members?
> 
>   You don't need them.

That's what I was pretty much hoping, yeah.

Looks like it would only be useful for some pretty obscure situations...


Post a reply to this message

From: Invisible
Subject: Re: C++ questions
Date: 26 Sep 2008 09:12:31
Message: <48dcdfbf$1@news.povray.org>
Warp wrote:
> Invisible <voi### [at] devnull> wrote:
>> Copy...constructor...? x_x
> 
>   Usually you don't need copy constructors unless you have pointers as
> member variables of your class and you explicitly allocate memory to
> them with 'new'. But we don't do that, do we? ;)

Heh. I get the impression that most of the stuff I'm reading about now 
is stuff that "you won't normally need". ;-)

(E.g., apparently you can overload the "->" operator... GAH! >_< OK, I 
am *so* not trying that out!)


Anyway, the main reason I haven't written a Third C++ Program yet is 
that I'm trying to think of something I actually want to write that's 
implementable without automatic memory management...


Post a reply to this message

From: Warp
Subject: Re: C++ questions
Date: 26 Sep 2008 09:34:51
Message: <48dce4fb@news.povray.org>
Invisible <voi### [at] devnull> wrote:
> Warp wrote:
> > Invisible <voi### [at] devnull> wrote:
> >> Pointers to members?
> > 
> >   You don't need them.

> That's what I was pretty much hoping, yeah.

> Looks like it would only be useful for some pretty obscure situations...

  They are useful in certain situations, but those situations are so rare
and usually so advanced, that even very experienced C++ programmers basically
never need them.

  I once created a command-line parameter interpreter which was used like
this (extremely simplified):

class MyCLI: public CommandLineInterpreter
{
    void inputFile() { ... }

 public:
    MyCLI()
    {
        // registerCLParameter() is a function in the base class.
        registerCLParameter("input", &inputFile);
    }
};

  Basically this meant that when the interpreter encountered the command-line
parameter "-input", it would call the 'inputFile()' method I defined above.
This uses method pointers.

  (The actual implementation is quite complicated because there are some
issues about a base class calling a derived class method through a method
pointer, but with template magic it's possible to do this portably and
safely.)

-- 
                                                          - 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.