Hi!,
I want to make a class that may do the following:
class T
{
......
}
int main()
{
T k = new T(3, 3);
/* k is now an array of 3, 3 integers */
k[2][1]=4; /* this is what I don't know how to do */
cout<<k[2][1];
return 0;
}
The trouble is that I don't know the keywords to search for to know
more about this. I thought 'base class' may be the way to describe
such a class so I posted it with the subject.
I hope someone helps this moron out,
-Craig 3 2542
"Craig Joyce" <na***********@ yahoo.com> wrote... I want to make a class that may do the following:
class T { ...... }
int main() { T k = new T(3, 3); /* k is now an array of 3, 3 integers */
k[2][1]=4; /* this is what I don't know how to do */ cout<<k[2][1]; return 0;
}
The trouble is that I don't know the keywords to search for to know more about this. I thought 'base class' may be the way to describe such a class so I posted it with the subject.
What you need is to implement a class that overloads the indexing
operator (operator[]) so that "elements" could be accessed using
it.
class T
{
vector<vector<i nt> > storage; // where the elements are
public:
T(int w, int h);
vector<int>& operator[](int);
const vector<int>& operator[](int) const;
};
That's a minimalist interface your 'T' should probably have. You
get to fill the gaps.
Now, when you create it, you will have to probably create an object
without using 'new':
T k(3,3);
Victor
"Craig Joyce" <na***********@ yahoo.com> wrote in message
news:7e******** *************** ***@posting.goo gle.com... Hi!,
I want to make a class that may do the following:
class T { ...... }
// replace "class T {...}" by the following:
#include<vector > int main() { T k = new T(3, 3);
// replace the above "T k = new T(3, 3);" by the following 4 lines:
std::vector<std ::vector<int> > k; // don't forget the extra space between
the two ">" ">"
k.resize(3);
k[0].resize(3);
k[1].resize(3);
k[2].resize(3);
/* k is now an array of 3, 3 integers */
k[2][1]=4; /* this is what I don't know how to do */ cout<<k[2][1];
// the above two lines work fine with std::vector<std ::vector<int> >
return 0;
}
The trouble is that I don't know the keywords to search for to know more about this.
Although it is not immediately obvious, the keyword to search for (as far as
standard C++ is concerned) would be 'vector'.
I thought 'base class' may be the way to describe such a class so I posted it with the subject.
There might exist a way in standard C++ to implement an array of 3, 3
integers using a base class, but this would probably be very complicated
compared to the std::vector<std ::vector<int> > solution.
I hope someone helps this moron out, -Craig
"Craig Joyce" <na***********@ yahoo.com> wrote in message
news:7e******** *************** ***@posting.goo gle.com... Hi!,
I want to make a class that may do the following:
class T { ...... }
int main() { T k = new T(3, 3); /* k is now an array of 3, 3 integers */
k[2][1]=4; /* this is what I don't know how to do */ cout<<k[2][1]; return 0;
}
The trouble is that I don't know the keywords to search for to know more about this. I thought 'base class' may be the way to describe such a class so I posted it with the subject. I hope someone helps this moron out, -Craig
Nothing to do with base classes, operator overloading and proxy classes are
what you want. In short you will overload the operator[] for T, to return a
proxy class which will also have the operator[] overloaded.
john This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: frs |
last post by:
For memory economization, I need to get rid if the virtual
destructor. Under this constraint I get caught up in a design,
where I need to call a destructor of a derived class from a
base class. Briefly it could be displayed like the code below.
It ends up deleting the member 'x' of 'B' twice. Why? Is there
a way around?
Thanks
Frank
|
by: Luca Bertoldi |
last post by:
Hi all,
I developed a base class that inherits from System.Web.UI.Page and I'd like
to check some cookies in the constructor of the class.
I tried with
if (Context.Request.Cookies == null) {
Context.Response.Write("<script language='javascript'>document.location='/"
+ currContext.Request.ApplicationPath + "/default.asp?msg=Please, log
on&goto=" + ModuleCode + "';</script>");
Context.Response.End();
|
by: martin |
last post by:
Hi,
If I have a base class, say called "MyBase" and a derived class called
"Myderived" is it possible to get the name of the derived class from the
base class
fo example if I have a method in "MyBase" called "fill" and this method is
not overridden in "Myderived" and I then create and object of my derived and
call the fill method in the base class, the is it possible for the for fill
method of mybase to know it was called from an...
|
by: Andy |
last post by:
Hi all,
I have a site with the following architecture:
Common.Web.dll - Contains a CommonPageBase class which inherits
System.Web.UI.Page
myadd.dll - Contains PageBase which inherits CommonPageBase
- Contains myPage which inherits PageBase
Each of these classes overrides OnInit and ties an event handler
|
by: anongroupaccount |
last post by:
What measures should be taken to avoid this sort of thing?
class Base
{
};
class Derived1 : public Base
{
private:
int i, j, k;
| |
by: Dennis Jones |
last post by:
Hello,
I have a couple of classes that look something like this:
class RecordBase
{
};
class RecordDerived : public RecordBase
{
|
by: bryanbabula |
last post by:
I have a question about overriding i was wondering if anyone could
help me with, or even suggesting a better/different way. I have no
idea if this can even be done or not.
I was wondering if there was anyway to force a class to call a base
class's method that it is overriding? Almost the same way you have to
call a base class's constructor if it has arguments.
(example ** assuming the Person class's constructor has (string
FirstName) as...
|
by: jan.loucka |
last post by:
Hi,
We're building a mapping application and inside we're using open
source dll called MapServer. This dll uses object model that has quite
a few classes. In our app we however need to little bit modify come of
the classes so they match our purpose better - mostly add a few
methods etc.
Example: Open source lib has classes Map and Layer defined. The
relationship between them is one to many.
We created our own versions (inherited) of Map...
|
by: Nagrik |
last post by:
Dear Group,
I am calling a function which takes a parameter of type base class and
passing a
concrete (compile time) derived class. The result is that it still
calls the base class
implementation and not the derived class.
For parameter passed as an object, it does not matter whether the
function is
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |