473,385 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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<iostream>
struct P {
int x;
int y;
};

int main() {
typedef std::vector<Pcontainer;
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 1269
On Mon, 04 Aug 2008 12:32:28 +0200, saneman <as***@asd.comwrote:
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<iostream>
struct P {
int x;
int y;
};

int main() {
typedef std::vector<Pcontainer;
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<Pcontainer; by
typedef std::list<Qcontainer; with struct Q {int x;} and it will works
fine.
Aug 4 '08 #2
"saneman" <as***@asd.comwrites:
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::doSomething();
ModuleB::doSomething();
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*******@wanadoo.frskrev i en meddelelse
news:op.ufczopz9rttu86@debian...
On Mon, 04 Aug 2008 12:32:28 +0200, saneman <as***@asd.comwrote:
>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<iostream>
struct P {
int x;
int y;
};

int main() {
typedef std::vector<Pcontainer;
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<Pcontainer; by
typedef std::list<Qcontainer; 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<iostream>
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.comsaid:
>
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.comwrites:
>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.comwrote:
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<iostream>
struct P {
int x;
int y;
};
int main() {
typedef std::vector<Pcontainer;
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 objektorientierter Datenverarbeitung
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
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...
3
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...
1
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...
2
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...
1
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...
8
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...
2
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 ...
4
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...
1
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...
5
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
jinu1996
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...

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.