Can function return an array?

This is a discussion on Can function return an array? within the C++ Programming forums, part of the General Programming Boards category; I would not say a const reference breaks encapsulation. If it returns const, it means "this data is not meant ...

  1. #16
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    I would not say a const reference breaks encapsulation. If it returns const, it means "this data is not meant to be modified by any outside code," and if the programmer breaks that, then it isn't the fault of the class and encapsulation is not threatened.
    The programmer who breaks the const is the one doing the wrong and when we remember that classes are objects which are typically separated from any outside code, this means that the class itself didn't do any wrong, but the outside code did, and this doesn't really hurt the encapsulation of the class, but rather a programming error, one that shouldn't be made because it's const.
    You're giving a clear message here and if the programmer who uses the class choose to ignore that, it's not your fault.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    You also have the potential of a dangling reference if the object goes out of scope. Sorry, that code wouldn't make it out of a code review if I had anything to say about it.

  3. #18
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    That is not justifiable to copying data, however. I think every programmer is smart enough to know that when taking a reference like this, they won't store it because the object will go out of scope.
    This function is also meant more to be for getting access to the member and not storing it (temporary access, not permanent).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #19
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by Elysia View Post
    That is not justifiable to copying data, however. I think every programmer is smart enough to know that when taking a reference like this, they won't store it because the object will go out of scope.
    Have you read the questions on this forum lately? This can and would get easily abused and is not worth the risk.

    The folly of premature optimization is bad enough without writing dangerous code.

  5. #20
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You also have the potential of a dangling reference if the object goes out of scope.
    Not really. Unless the reference is a member of a class and you allocate an object of the class on the heap, giving the reference to the constructor, the reference has to be a local variable. Since the object can go out of scope, it's gotta be a local variable too. Since references have to be bound at initialization time, it's guaranteed that the object lives longer than the reference. And if you do the whole allocation thing, it's your code that won't pass review.

    What returning a reference does break is the ability to change the underlying storage.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #21
    Registered User
    Join Date
    Apr 2008
    Posts
    17
    Thanks for all the replies. To be honest I have little to no experience with vectors so I will stay away from those for now. And most of what you guys said was way over my head.

    I ended up doing away getAbilities(). Instead I just declared a private string mobAbilities[x] where x is the total number of all spells I plan on using (not just the ones a monster is 'allowed' to cast). I set up CastSpell() member function to just roll a random number for only the spells they can use. It should work for now...

    Code:
    string Mobs::CastSpell() {
    if (mobName == "Troll Mage") {
         mobAbilities[x] = {"MageSpell1", "MageSpell2", "Spell3",
    "WarriorSpell1","WarriorSpell2"...etc}
         spellCasted = mobAbilities[rand() % 3];
    I can already see a future problem, like if a monsters can cast MageSpell1, MageSpell3 and WarriorSpell2....not sure how I can randomize that.

    I might have to finally start looking at vectors and take the advice about them.

    Thanks again.

  7. #22
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    I suggest you do. In practice, they're very easy to use (because they are typically used the same as C arrays, with some exceptions). They're the backbone of arrays in C++.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21