Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Poll

What programming topic would you want the next challenge to be about?  (It might be a good opportunity to focus on a subject you're not familiar with or to reinforce knowledge on one that you already know)

Control Flow
- 2 (2.2%)
Arrays, Strings, Pointers, and References
- 8 (9%)
Functions
- 4 (4.5%)
Basic object-oriented programming
- 30 (33.7%)
A bit more advanced OOP (Composition, Operator overloading, Inheritance, Virtual Functions)
- 18 (20.2%)
Templates
- 8 (9%)
Other (Explain)
- 4 (4.5%)
Working with files?  (Streams)
- 15 (16.9%)

Total Members Voted: 89


Pages: 1 ... 32 33 [34] 35 36 ... 78

Author Topic: Programming Challenges & Resources (#bay12prog) Initiative  (Read 96029 times)

Normandy

  • Bay Watcher
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #495 on: December 04, 2010, 02:03:32 pm »

Huh? Why are the Java I/O capabilities failing you? Use the built-in readers/writers and not much can go wrong.

Also, the problem is not in initialization. There's a problem somewhere else in your code. Check your loop bounds. And check to make sure your code will actually compile (you have a syntax error in the code you pasted) before posting it on the forum.
Logged

eerr

  • Bay Watcher
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #496 on: December 04, 2010, 05:18:33 pm »

"new RGB(0, 0, 0)[hght*widt];"
This is not valid syntax!

This will not work.

You cannot do that.

The result of RGB(0,0,0) is an RGB object.
The RGB object is not an array, and therefore fails any function of []

x= new RGB(0,0,0);
x[hght*widt];

will produce an error, and is the closest interpretation to what you are trying to do.

perhaps you should try this page?

http://download.oracle.com/javase/1.4.2/docs/api/java/awt/Graphics.html
http://download.oracle.com/javase/1.4.2/docs/api/java/awt/Image.html
Logged

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #497 on: December 04, 2010, 05:25:34 pm »

"new RGB(0, 0, 0)[hght*widt];"
This is not valid syntax!
True, but the problem is not where you're looking for it. You can't call the constructor like that when initializing an array of objects. Since you're initializing them all to 0,0,0 anyway, you could define a default constructor that does just that. Alternatively:
Code: [Select]
RGB[] foo;
for(int i = 0; i<(hght*widt); i++)
{
    foo[i] = new RGB(0,0,0);
}
should also work[/quote]
« Last Edit: December 04, 2010, 05:35:56 pm by Virex »
Logged

eerr

  • Bay Watcher
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #498 on: December 04, 2010, 05:51:02 pm »

"new RGB(0, 0, 0)[hght*widt];"
This is not valid syntax!
True, but the problem is not where you're looking for it. You can't call the constructor like that when initializing an array of objects. Since you're initializing them all to 0,0,0 anyway, you could define a default constructor that does just that. Alternatively:
Code: [Select]
RGB[] foo;
for(int i = 0; i<(hght*widt); i++)
{
    foo[i] = new RGB(0,0,0);
}
should also work
[/quote]

foo is null, but otherwise that will work.
Logged

Normandy

  • Bay Watcher
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #499 on: December 04, 2010, 08:46:46 pm »

When you create an array (i.e. new Object[]) all values of the array are initialized to null, not to the default constructor. Keep that in mind.
Logged

Supermikhail

  • Bay Watcher
  • The Dwarf Of Steel
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #500 on: December 05, 2010, 03:40:51 am »

I'm studying C++ by a book, and I'm kind of lost. Help! Can somebody explain to me why there's *const in a custom String class constructor?
Code: [Select]
String( const char *const );
Is it because you don't want the data that the pointer points to changed, as well as the pointer itself?

Also, do I understand it right? const goes after the parentheses when you don't want a function to change anything?
Code: [Select]
char operator[] ( int offset ) const;
What about before the return value?
Code: [Select]
const char * GetString () const { return itsString; }

I think the placement of const has been the most confusing thing so far, after pointers and references in function declarations.
Logged

Normandy

  • Bay Watcher
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #501 on: December 05, 2010, 05:21:56 pm »

Part of this confusion stems from the fact that const char and char const are exactly the same thing. Normally, modifiers work right-to-left, i.e. char const**&* will be a pointer to a reference to a pointer to a pointer to a const char. However, because of readability concerns, const also works like const char**&*. However, when you're talking about doing something like char const* const*&* then you're talking a pointer to a reference to a pointer to a const pointer to a const char.

When declaring function prototypes, you don't need to enter the variable names. In String( const char *const ); the const char *const is simply an argument. You might find it better to think of it like String( const char *const str );. As noted, a const char *const is a const pointer to a const char, also known as a string literal. When you type something like: const char* str = "Hello World";, the string literal is of type const char*. The reason the argument is const char *const is because you can convert from a non-const to a const, but not the other way around. Since the function doesn't need to change either the pointer or the value it's pointing to, it is constant.

As for your second question, correct, that's when the function will not change anything. When it's before the return value, it simply makes the return value const.
Logged

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #502 on: December 06, 2010, 08:07:47 am »

Or, the short answer, literals are always const, so you can use

Code: [Select]
String a = new String("hello");
"hello" is a literal char*, and as such is always const. So indeed, both the data and the pointer should not be changed. Const is something of a security flag, so for instance
Code: [Select]
function a(int& number) {}
a(4)

Is illegal, as you reference the parameter, but as it's a const, you may not change it. Copying every parameter is an option, but might slow down your program (especially when you use large objects as parameters).


Or something like that.
Logged

This one thread is mine. MIIIIINE!!! And it will remain a happy, friendly, encouraging place, whether you lot like it or not. 
will rena,eme sique to sique sxds-- siquo if sucessufil
(cant spel siqou a. every speling looks wroing (hate this))

Supermikhail

  • Bay Watcher
  • The Dwarf Of Steel
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #503 on: December 06, 2010, 09:59:10 am »

Err.

Normandy, nobody uses such constructions, right? Or do they?

Siquo: What I don't exactly understand - "hello" is a literal char* . But then it's a value with which String is created... Wait. So in that segment first a char* is created, then a new String object is created, and then the char* is copied into the String? I mean what comes first - the char* or the String? Isn't "hello" the value of some variable and so easily changeable?

I must be wrong.
Logged

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #504 on: December 06, 2010, 10:24:06 am »

If it's a literal, it's not the value of some variable (well, under water it is).

The value passed to String is a char*, or: an integer with an address value pointing to the first letter of that string, where the string resides in a temporarily reserved block. Copying char* only copies the address pointing to that first character, not the string itself. Since the string itself is a literal, the address pointer and pointee are both const.

String takes the char* address, and starts reading chars from it until it reaches a null-char, meaning it's the end-of-string. It then (or simultaneously or whenever) copies those chars into its own char array, which it can modify, because the original address block can't be used as the memory will be released as soon as the literal is out-of-scope.

If I remember correctly.

Logged

This one thread is mine. MIIIIINE!!! And it will remain a happy, friendly, encouraging place, whether you lot like it or not. 
will rena,eme sique to sique sxds-- siquo if sucessufil
(cant spel siqou a. every speling looks wroing (hate this))

qwertyuiopas

  • Bay Watcher
  • Photoshop is for elves who cannot use MSPaint.
    • View Profile
    • uristqwerty.ca, my current (barren) site.
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #505 on: December 06, 2010, 10:30:34 am »

Any time you use quoted text in C/C++, it is converted to a pointer (that is why adding two strings together, in the absence of operator overloading,  gives rather unexpected results), and the actual string is probably stored as read-only, if possible. If it is defined as char[], however, it will be writable.
See http://stackoverflow.com/questions/2589949/c-string-literals-where-do-they-go.
Logged
Eh?
Eh!

Supermikhail

  • Bay Watcher
  • The Dwarf Of Steel
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #506 on: December 06, 2010, 10:57:53 am »

What? I'm pretty sure I read in one book that char * and char [] are interchangeable, that char[] is still a pointer to the first member. Did they trick me again? :'(
Logged

Rose

  • Bay Watcher
  • Resident Elf
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #507 on: December 06, 2010, 11:25:46 am »

char * and char[] mean the same thing, more or less

but they aren't the same as "hello" or const char *
Logged

Supermikhail

  • Bay Watcher
  • The Dwarf Of Steel
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #508 on: December 06, 2010, 11:54:05 am »

Okay. Then, I believe, qwertyuiopas is trying to confuse me further, villainously. :'(
Any time you use quoted text in C/C++, it is converted to a pointer (that is why adding two strings together, in the absence of operator overloading,  gives rather unexpected results), and the actual string is probably stored as read-only, if possible. If it is defined as char[], however, it will be writable.
See http://stackoverflow.com/questions/2589949/c-string-literals-where-do-they-go.
How is adding strings together related to literal character arrays? Strings are supposed to be objects of the String class, aren't they? And how can you define quoted text as char[]? That I don't understand at all.
Logged

Rose

  • Bay Watcher
  • Resident Elf
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #509 on: December 06, 2010, 12:08:54 pm »

there's cstrings, which is just a char array, and there's the C++ string class, which is an actual class, with overloaded operators in whatnot.

if something converts to a cstring, the generally refer to char arrays.
Logged
Pages: 1 ... 32 33 [34] 35 36 ... 78