If you wanted to put int,double into your own container class would you do it?

  • Thread starter Thread starter SpiffyEh
  • Start date Start date
  • Tags Tags
    Class Container
Click For Summary

Discussion Overview

The discussion revolves around the implementation of a custom container class that can hold various data types such as integers, doubles, and strings. Participants explore different programming languages and approaches to achieve this, focusing on aspects like type safety and memory management.

Discussion Character

  • Exploratory
  • Technical explanation
  • Conceptual clarification

Main Points Raised

  • One participant suggests that the choice of programming language is crucial for implementing a container class.
  • Another participant emphasizes the importance of defining the size and orientation of data types when creating a container class.
  • A suggestion is made to use typedefs in C/C++ to ensure consistent data type sizes across different platforms, particularly for integers.
  • Concerns are raised about the handling of strings, specifically whether they are fixed length or allocated on the heap, which affects how they can be stored in a structure.
  • In response to a clarification that the implementation is in Java, a participant recommends using generics and mentions specific classes like List, ArrayList, and Vector as potential solutions.

Areas of Agreement / Disagreement

Participants express differing views on the best approach to implement the container class, with some focusing on C/C++ specifics while others advocate for Java solutions. The discussion remains unresolved regarding the optimal method for handling various data types across different programming languages.

Contextual Notes

There are limitations regarding assumptions about platform dependencies for data types and the implications of memory allocation for strings, which are not fully explored in the discussion.

Technology news on Phys.org
It might help if you said which language you are talking about.
 
SpiffyEh said:
If you wanted to put ints, doubles, strings into your own container class how would you do it?

Aside from CompuChip's suggestion, you also need to define the size and the orientation of your "words".

My suggestion to you is if you are doing your development in something C/C++, you should typedef your doubles, ints, and strings so that they are always in the same format regardless of the platform.

Typically what happens is that an int is platform dependent. On a 32-bit platform is 32 bits and on a 64-bit platform it is 64-bits. The best way to get around this is to define something like INT32 which is guaranteed to 32 bits regardless of what platform you execute the code on.

Also with strings, are they fixed length or are they allocated on the heap? If they are allocated on the heap, then your string data will not be able to stored in your structure as a TYPE or struct definition. One way around this is to use a large flat area of memory that is the maximum size of a string. Languages like BASIC can do this, and it's very easy to do this with a struct definition in C/C++.
 
Sorry, I forgot to mention I am trying to do it in Java
 
Then you can use generics. Look into List<A>, ArrayList<T>, Vector<T>, etc.
 

Similar threads

  • · Replies 36 ·
2
Replies
36
Views
6K
  • · Replies 35 ·
2
Replies
35
Views
4K
  • · Replies 5 ·
Replies
5
Views
1K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 36 ·
2
Replies
36
Views
3K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 36 ·
2
Replies
36
Views
3K