POV-Ray : Newsgroups : povray.off-topic : C++ questions Server Time
7 Sep 2024 05:13:33 EDT (-0400)
  C++ questions (Message 21 to 30 of 123)  
<<< Previous 10 Messages Goto Latest 10 Messages Next 10 Messages >>>
From: Invisible
Subject: Re: C++ questions
Date: 25 Sep 2008 04:23:59
Message: <48db4a9f$1@news.povray.org>
Slime wrote:
> Man, I'm in the wrong time zone. You always get to answer the questions 
> first! =)

Which timezone is that then?


Post a reply to this message

From: John VanSickle
Subject: Re: C++ questions
Date: 25 Sep 2008 07:32:43
Message: <48db76db@news.povray.org>
Invisible wrote:
> (You knew there were going to be a few at some point...)
> 
> I just want to make sure I've got this absolutely straight in my head. 
> So... a reference is the same as a pointer, except that it has nicer 
> syntax, and you cannot change where it points to?
> 
> If I'm understanding this correctly, a "union" is like several structs 
> with the same base address, and you can treat it was one struct or the 
> other struct, and it's up to you to remember which which struct you're 
> currently using it is. (I.e., the language itself provides no way to 
> distinguish.)
> 
> Here's a perverse question: can a union have member functions?

Yes.

> My understanding is that when you create variables, they start off 
> containing junk unless you initialise them (or their types have 
> constructors which initialise them to something specific). Is that correct?

Yes.

> Does C++ have a concept of a "null pointer" - i.e., a pointer that 
> doesn't point to anything valid, and can be detected as such?

Yes.

> How does memory allocation work in C++? If a program fills the heap, do 
> you have to explicitly *do* something to enlarge the heap, or does it 
> grow automatically? Does it shrink back again after you release things, 
> or do you have to request that manually?

Depends on the implementation.

Regards,
John


Post a reply to this message

From: Darren New
Subject: Re: C++ questions
Date: 25 Sep 2008 17:46:38
Message: <48dc06be@news.povray.org>
Slime wrote:
>> More exactly, in C at least, any variables allocated statically start with 
>> a default value of zero appropriate for their type. That includes static 
>> variables allocated inside a function.
> 
> 
> I was under the impression that global variables are initialized to zero, 
> but you can't trust local variables because the stack has whatever garbage 
> data was left on it from previously called functions.

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

And variables that are static but not global also get initialized.

-- 
Darren New / San Diego, CA, USA (PST)


Post a reply to this message

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

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

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