473,626 Members | 3,216 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Type visibility

I have made two modules:

1) main.cpp
Contains a main function and struct P.
Includes head.h and std::vector

#include "head.h"
#include<vector >
#include<iostre am>
struct P {
int x;
int y;
};

int main() {
typedef std::vector<Pco ntainer;
container v;
P p;
p.x = 22;
p.y = 33;
v.push_back(p);
A<containera(v) ;
int res = a.getValue();
std::cout << res << std::endl;
return 0;
}


2) head.h
A header file that contains the class A.

template <typename T >
class A {
public:
T t;

A(T t_){
t = t_;
}
int getValue() {
int res = (*t.begin()).x;
return res;

}
};


In class A getValue() returns the x field of the struct defined in main.cpp.
But how does class A know about the type 'P' which is only created in main?
Aug 4 '08 #1
7 1276
On Mon, 04 Aug 2008 12:32:28 +0200, saneman <as***@asd.comw rote:
I have made two modules:

1) main.cpp
Contains a main function and struct P.
Includes head.h and std::vector

#include "head.h"
#include<vector >
#include<iostre am>
struct P {
int x;
int y;
};

int main() {
typedef std::vector<Pco ntainer;
container v;
P p;
p.x = 22;
p.y = 33;
v.push_back(p);
A<containera(v) ;
int res = a.getValue();
std::cout << res << std::endl;
return 0;
}


2) head.h
A header file that contains the class A.

template <typename T >
class A {
public:
T t;

A(T t_){
t = t_;
}
int getValue() {
int res = (*t.begin()).x;
return res;

}
};


In class A getValue() returns the x field of the struct defined in
main.cpp.
But how does class A know about the type 'P' which is only created in
main?

A doesn't know P. He knows just T must have a public member named x.
Replace typedef std::vector<Pco ntainer; by
typedef std::list<Qcont ainer; with struct Q {int x;} and it will works
fine.
Aug 4 '08 #2
"saneman" <as***@asd.comw rites:
I have made two modules:
Good.

1) main.cpp
2) head.h
Wrong.
If you have two modules, you should have five files:

interface implementation
Module A: ModuleA.h ModuleA.cpp
Module B: ModuleB.h ModuleB.cpp
Main: main.cpp

Assuming ModuleB uses ModuleA, and main uses both modules, you will have:

------(ModuleA.h)-----------------------
#ifndef ModuleA_h
#define ModuleA_h

// public definitions of module A

#endif
----------------------------------------
------(ModuleA.cpp)---------------------
#include <ModuleA.h>

// implementation of module A

----------------------------------------

------(ModuleB.h)-----------------------
#ifndef ModuleB_h
#define ModuleB_h

#include <ModuleA.h>

// public definitions of module B

#endif
----------------------------------------
------(ModuleB.cpp)---------------------
#include <ModuleB.h>

// implementation of module B

----------------------------------------
------(main.cpp)------------------------
#include <ModuleA.h>
#include <ModuleB.h>

// implementation of main:
int main(int argc,char** argv,char** envv){
ModuleA::doSome thing();
ModuleB::doSome thing();
return(0);
}

----------------------------------------

Note that you cannot have cross dependencies between the interfaces of
the modules, but you may have cross dependencies between
implementations and interfaces:

ModuleC.cpp could include ModuleD.h
and ModuleD.cpp could include ModuleC.h

this would mean that both modules C and D are at the same layer.

Otherwise the dependencies between the modules correspond to the
layers structuring the software.

--
__Pascal Bourguignon__
Aug 4 '08 #3

"David Côme" <da*******@wana doo.frskrev i en meddelelse
news:op.ufczopz 9rttu86@debian. ..
On Mon, 04 Aug 2008 12:32:28 +0200, saneman <as***@asd.comw rote:
>I have made two modules:

1) main.cpp
Contains a main function and struct P.
Includes head.h and std::vector

#include "head.h"
#include<vecto r>
#include<iostr eam>
struct P {
int x;
int y;
};

int main() {
typedef std::vector<Pco ntainer;
container v;
P p;
p.x = 22;
p.y = 33;
v.push_back(p);
A<containera(v) ;
int res = a.getValue();
std::cout << res << std::endl;
return 0;
}


2) head.h
A header file that contains the class A.

template <typename T >
class A {
public:
T t;

A(T t_){
t = t_;
}
int getValue() {
int res = (*t.begin()).x;
return res;

}
};


In class A getValue() returns the x field of the struct defined in
main.cpp.
But how does class A know about the type 'P' which is only created in
main?


A doesn't know P. He knows just T must have a public member named x.
Replace typedef std::vector<Pco ntainer; by
typedef std::list<Qcont ainer; with struct Q {int x;} and it will works
fine.
But if the struct contains more fields the only way to make it work is using
templates right?
Aug 4 '08 #4
On 2008-08-04 12:32, saneman wrote:
I have made two modules:

1) main.cpp
Contains a main function and struct P.
Includes head.h and std::vector

#include "head.h"
#include<vector >
#include<iostre am>
I would include my own headers after the standard headers.
2) head.h
A header file that contains the class A.

template <typename T >
class A {
public:
T t;

A(T t_){
t = t_;
}
int getValue() {
int res = (*t.begin()).x;
return res;

}
};

In class A getValue() returns the x field of the struct defined in main.cpp.
But how does class A know about the type 'P' which is only created in main?
Because of how the preprocessor and #include works. When the pre-
processor sees the #include line it replaces that line with the contents
in the specified fields. This means that when the compiler gets main.cpp
it contains first the definition of A (followed by contents of vector
and iostream) and then P and finally main(). So when the compiler tries
to instantiate A it know everything about both A and P.

--
Erik Wikström
Aug 4 '08 #5
On 2008-08-04 06:32:28 -0400, "saneman" <as***@asd.coms aid:
>
2) head.h
A header file that contains the class A.

template <typename T >
class A {
public:
T t;

A(T t_){
t = t_;
}
int getValue() {
int res = (*t.begin()).x;
return res;

}
};


In class A getValue() returns the x field of the struct defined in main.cpp.
But how does class A know about the type 'P' which is only created in main?
"A" is not a class. It is a template. At the point where the code says
A<containerall the details are known, and the compiler creates the
class A<container>.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Aug 4 '08 #6
On 2008-08-04 13:39, Pascal J. Bourguignon wrote:
"saneman" <as***@asd.comw rites:
>I have made two modules:

Good.

>1) main.cpp
2) head.h

Wrong.
If you have two modules, you should have five files:

interface implementation
Module A: ModuleA.h ModuleA.cpp
Module B: ModuleB.h ModuleB.cpp
Main: main.cpp
Or, if you count main as a module you only need on header:

interface implementation
Module A: ModuleA.h ModuleA.cpp
Main: main.cpp

--
Erik Wikström
Aug 4 '08 #7
On Aug 4, 12:32 pm, "saneman" <as...@asd.comw rote:
I have made two modules:
1) main.cpp
Contains a main function and struct P.
Includes head.h and std::vector
#include "head.h"
#include<vector >
#include<iostre am>
struct P {
int x;
int y;
};
int main() {
typedef std::vector<Pco ntainer;
container v;
P p;
p.x = 22;
p.y = 33;
v.push_back(p);
A<containera(v) ;
int res = a.getValue();
std::cout << res << std::endl;
return 0;
}
2) head.h
A header file that contains the class A.
template <typename T >
class A {
public:
T t;

A(T t_){
t = t_;
}
int getValue() {
int res = (*t.begin()).x;
return res;
}
};
In class A getValue() returns the x field of the struct
defined in main.cpp. But how does class A know about the type
'P' which is only created in main?
As Pete said, there is no class A, only a class template A (and
later, a class A<container>). When parsing a template
definition, the compiler divides all names and expressions into
dependent and non-dependent; anything which is dependent only
gets looked up when the template is instantiated. The rules as
to when something is dependent, and when it's not, are fairly
complicated, as are the rules concerning dependent name look-up.
I'd suggest you get a good book about templates, such as "C++
Templates: the Complete Guide" (by Vandevoorde and Josuttis),
and study it carefully.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Aug 4 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2809
by: Jonathan | last post by:
Hi, I've read through quite a number of postings on here so far and have seen what look like very simply, reasonable answers to this question, however I am still completely unable to do what I want to do. I just want to know how I should toggle the visibility of divs in Netscape (I'm using 7). For example, say I have the following HTML code:
3
29673
by: Jukka K. Korpela | last post by:
I have noticed that the meaning of visibility: collapse has been discussed on different forums, but with no consensus on what it really means. Besides, implementations differ. The specification says: "The 'visibility' property takes the value 'collapse' for row, row group, column, and column group elements. This value causes the entire row or column to be removed from the display, and the space normally taken up by the row or column to...
1
6388
by: Micky Pearce | last post by:
Hi I have a page containing a hidden <div> called 'controls' plus an <iframe> containing a second page. I need to be able to set the visibility of the 'controls' div when the page in the iframe loads. Is this possible? Using this code I can change the visibility from a link in the parent window: onClick="document.getElementById('controls').style.visibility='visible'"
2
1499
by: vastaso | last post by:
Ciao, I have an hidden field and I want make it visible, setting its "type" to "text" from a popup window. I'm using this code: window.opener.document.forms.elements.type ='text'; it works fine in Mozilla but fails in Internet Explorer 6. Any ideas? Thanks
1
1784
by: lkrubner | last post by:
In the first version of this function, I only test for true or false, on a global var, without limiting that true or false to the id of a particular item. What this meant was that if you were using this function for several items on a page, sometimes you had to click twice and other times you only had to click once, because sometimes visibility was false though you were clicking on an item that was true. So then I started adding the id's...
8
3367
by: TTroy | last post by:
I have a few questions about "scope" and "visibility," which seem like two different things. To me "visibility" of the name of a function or object is the actual code that can use it in an actual program. To me "scope" of the name of a function or object are the general rules for the areas of a program that can through a declaration, have "visibility."
2
1699
by: Martin Eyles | last post by:
Hi, I am using Page.FindControl in a loop to select some numbered tags aTag = Page.FindControl("Tick_" + CStr(aNumber)) however, when I come to add a style to this aTag.Style.Add("visibility", "visible")
4
1430
by: Colin Desmond | last post by:
Is it possible to write an attribute that changes the visibility of a class (public/private) etc based on some pre-compiler define symbol? We're writing some classes which will be called by an external (bespoke) test harness, so we want then to be public in the assembly when we're testing, and private in the class the rest of the time. For example
1
1275
by: The Menace | last post by:
I have a problem that I've been trying to figure out. I have several controls on the page and depending on the selection on the drop down list, the visibility is set to control to true (set to default visibility to false) of some controls. What I want though is to make the control go up if the visibility is set to true instead of "stacked". I hope I'm explaining my problem correctly. I wanted to make the controls more dynamic that is...
5
1880
by: ballygowanboy | last post by:
hi, i've got 5 thumbnail images. each one will have a brief decription, in a layer below it, set to visibility:hidden; when the user mouse overs each image, the info will be displayed. i got the first one working using 2 functions, one to apear and one to disapear. how do i keep my script simple, and just use these functions to do the same effect on the other 4 images.
0
8268
marktang
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...
0
8202
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,...
1
8366
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,...
0
7199
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6125
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5575
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();...
0
4202
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2628
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1812
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.