Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Where are your objects stored in C++?

If you declare a variable to be of an object type in C++ (and potentially do so with parameters), you're going to be assigning memory for the variables that it includes on the stack - i.e. within memory that will be lost when you exit the closure (block of code - function - method) in which you are defining it.

BUT if you declare a pointer to a variable of a certain type, and then fill the variable with data using the new keyword, then only the pointer to that object is in local space on the stack, and the data itself will be on the heap. This means that if you return the pointer, or copy it into a more permanent piece of storage, you will retain the data that it contains even when the original closure has been completed.

In other words, you really want to construct your objects via new most of the time, the exception being where you have an object that's only going to have a very local and limited lifetime.

The notation for accessing a method within an object uses a dot - for example:
  savoy.setoccu(0.43);
and if you extend that to reference a method within an object for whcih you hold a pointer, you get the somewhat ugly form:
  (*ritz).setoccu(0.76);
You're provided with "syntactic icing" in C++ to make this prettier using the -> operator instead, thus:
  ritz->setoccu(0.76);

There's in example that sets up a hotel called "savoy" to hold data about a hotel, and a pointer to a hotel called "ritz" pointing to data on the heap [here]; you can find the source code of the class it uses [here] and the header file included in both [here].
(written 2010-01-16)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
C232 - C and C based languages - Defining and using classes in C++
  [1925] Introduction to Object Oriented Programming - (2008-12-06)
  [2577] Complete teaching example - C++, inheritance, polymorphism - (2010-01-15)
  [2579] Creating, setting up and using objects in C++ - (2010-01-16)
  [3250] C++ - how we teach the language and the concepts behind the language - (2011-04-17)
  [3716] Learning C++ - a design pattern for your first class - (2012-05-02)
  [3721] Naming blocks of code, structures and Object Orientation - efficient coding in manageable chunks - (2012-05-06)
  [3810] Reading files, and using factories to create vectors of objects from the data in C++ - (2012-07-21)
  [3978] Teaching OO - how to avoid lots of window switching early on - (2013-01-17)
  [4129] Simple OO demonstration in C++, comparison to Python - (2013-07-01)
  [4372] Template / design pattern for C++ constructor and accessors - (2014-12-29)
  [4565] Allocation of memory for objects in C++ - Stack v Heap - (2015-10-31)


Back to
Complete teaching example - C++, inheritance, polymorphism
Previous and next
or
Horse's mouth home
Forward to
Creating, setting up and using objects in C++
Some other Articles
Starting to arrange the picture library
Snow scenes - and how snow effected our business
C course inspires new teaching examples
Where are your objects stored in C++?
What does const mean? C and C++
Sharing variables between files of code in C - extern
Summary of Wiltshire Core Strategy responses
C Structs - what, how and why
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.info/mouth/2578_Whe ... in-C-.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb